Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE MATERIALIZED VIEW stats.daily_dashboard_user_report AS(
- WITH new_users_day_before_today_cte AS(
- SELECT
- total AS new_users_day_before_today
- FROM
- newUsersDaily
- ORDER BY
- date_time DESC
- LIMIT
- 1
- OFFSET
- 1
- ), new_users_today_cte AS(
- SELECT
- total AS new_users_today
- FROM
- newUsersDaily
- ORDER BY
- date_time DESC
- LIMIT
- 1
- ), new_accounts_today_cte AS(
- SELECT
- count(*) AS new_accounts_today
- FROM
- usersAndTokens
- WHERE
- created_time BETWEEN DATE( NOW() + INTERVAL '+8 hour' + INTERVAL '-1 day') - '8h'::interval AND DATE( NOW() + INTERVAL '+8 hour') - '8h'::interval
- LIMIT
- 1
- ), new_accounts_before_today_cte AS(
- SELECT
- count(*) AS new_accounts_before_today
- FROM
- usersAndTokens
- WHERE
- created_time BETWEEN DATE( NOW() + INTERVAL '+8 hour' + INTERVAL '-2 day') - '8h'::interval AND DATE( NOW() + INTERVAL '+8 hour' + INTERVAL '-1 day') - '8h'::interval
- LIMIT
- 1
- ), new_avg_accounts_before_today_cte AS(
- SELECT
- (count(*) / 7)::int as new_avg_accounts_before_today
- FROM
- usersAndTokens
- WHERE
- created_time BETWEEN DATE( NOW() + INTERVAL '+8 hour' + INTERVAL '-8 day') - '8h'::interval AND DATE( NOW() + INTERVAL '+8 hour' + INTERVAL '-1 day') - '8h'::interval
- LIMIT
- 1
- ), new_accounts_weekday_before_cte AS(
- SELECT
- count(*) AS new_accounts_weekday_before
- FROM
- usersAndTokens
- WHERE
- created_time BETWEEN DATE( NOW() + INTERVAL '+8 hour' + INTERVAL '-8 day') - '8h'::interval AND DATE( NOW() + INTERVAL '+8 hour' + INTERVAL '-7 day') - '1s 8h'::interval
- LIMIT
- 1
- ),new_matches_before_today_cte AS(
- SELECT
- total AS new_matches_before_today
- FROM
- newMatchesDaily
- ORDER BY
- date_time DESC
- LIMIT
- 1
- OFFSET
- 1
- ), new_matches_today_cte AS(
- SELECT
- total AS new_matches_today
- FROM
- newMatchesDaily
- ORDER BY
- date_time DESC
- LIMIT
- 1
- ), total_users_today_cte AS(
- SELECT
- totalRegistredUsers.total - COALESCE( totalRegistredHiddenUsers.total, 0) AS non_hidden_users_total
- FROM
- totalRegistredUsers
- LEFT JOIN
- totalRegistredHiddenUsers USING(date_time)
- ORDER BY
- date_time DESC
- LIMIT
- 1
- ), total_accounts_today_cte AS(
- SELECT
- total AS total_accounts_today
- FROM
- totalRegistredUsers
- ORDER BY
- date_time DESC
- LIMIT
- 1
- ), daily_active_users_today_cte AS(
- SELECT
- total AS daily_active_users_today
- FROM
- dailyActiveUsers
- ORDER BY
- date_time DESC
- LIMIT
- 1
- ), daily_active_users_before_today_cte AS(
- SELECT
- total AS daily_active_users_before_today
- FROM
- dailyActiveUsers
- ORDER BY
- date_time DESC
- LIMIT
- 1
- OFFSET
- 1
- ), weekly_active_users_today_cte AS(
- SELECT
- total AS weekly_active_users_today
- FROM
- weeklyActiveUsers
- ORDER BY
- date_time DESC
- LIMIT
- 1
- ), weekly_active_users_before_today_cte AS(
- SELECT
- total AS weekly_active_users_before_today
- FROM
- weeklyActiveUsers
- ORDER BY
- date_time DESC
- LIMIT
- 1
- OFFSET
- 1
- ), monthly_active_users_today_cte AS(
- SELECT
- total AS monthly_active_users_today
- FROM
- monthlyActiveUsers
- ORDER BY
- date_time DESC
- LIMIT
- 1
- ), monthly_active_users_before_today_cte AS(
- SELECT
- total AS monthly_active_users_before_today
- FROM
- monthlyActiveUsers
- ORDER BY
- date_time DESC
- LIMIT
- 1
- OFFSET
- 1
- )
- SELECT
- new_accounts_today,
- new_users_today,
- new_matches_today,
- non_hidden_users_total,
- total_accounts_today,
- daily_active_users_today,
- weekly_active_users_today,
- monthly_active_users_today,
- 100*new_accounts_today::numeric/NULLIF(new_accounts_before_today, 0) - 100 AS new_acc_percent,
- 100*new_accounts_today::numeric/NULLIF(new_accounts_weekday_before, 0) - 100 AS new_acc_weekday_percent,
- 100*new_accounts_today::numeric/NULLIF(new_avg_accounts_before_today, 0) - 100 AS new_avg_acc_percent,
- 100*new_users_today::numeric/NULLIF(new_users_day_before_today, 0) - 100 AS tru_percent,
- 100*new_matches_today::numeric/NULLIF(new_matches_before_today, 0) - 100 AS matches_percent,
- 100*daily_active_users_today::numeric/NULLIF(daily_active_users_before_today, 0) - 100 AS dau_percent,
- 100*weekly_active_users_today::numeric/NULLIF(weekly_active_users_before_today, 0) - 100 AS wau_percent,
- 100*monthly_active_users_today::numeric/NULLIF(monthly_active_users_before_today, 0) - 100 AS mau_percent
- FROM
- new_users_day_before_today_cte,
- new_users_today_cte,
- new_accounts_today_cte,
- new_accounts_before_today_cte,
- new_avg_accounts_before_today_cte,
- new_accounts_weekday_before_cte,
- new_matches_before_today_cte,
- new_matches_today_cte,
- total_users_today_cte,
- total_accounts_today_cte,
- daily_active_users_today_cte,
- daily_active_users_before_today_cte,
- weekly_active_users_today_cte,
- weekly_active_users_before_today_cte,
- monthly_active_users_today_cte,
- monthly_active_users_before_today_cte
- )
Add Comment
Please, Sign In to add comment