WITH
u AS (SELECT user_id,
COUNT(user_id) OVER () AS total_users
FROM tools_shop.users
WHERE created_at::date BETWEEN '2020-01-01' AND '2020-01-31'),
e AS (SELECT *
FROM tools_
shop.events WHERE event_time::date BETWEEN '2020-01-01' AND '2020-03-31'
AND platform = 'android')
SELECT CAST(DATE_TRUNC('month', event_time)AS timestamp) AS dat,
total_users,
COUNT(DISTINCT e.user_id) AS unic
FROM e
INNER JOIN u ON e.user_id = u.user_id
GROUP BY CAST(DATE_TRUNC('month', event_time)AS timestamp)
,total_users
ORDER BY CAST(DATE_TRUNC('month', event_time)AS timestamp),
total_users;
таблицы:
Мое решение:
SELECT *
FROM tools_ shop.events t1
INNER join (select user_id,
count (*) over () as total
from tools_shop.users
where date_trunc ('month', created_at) = '2020-01-01') t2 on t1.user_id = t2.user_id
AND platform = 'android'
AND DATE_TRUNC('month',event_time::timestamp) BETWEEN '2020-01-01' AND '2020-03-01'
ORDER BY DATE_TRUNC('month',event_time::timestamp), total