Group by day week or month » History » Version 1
Viacheslav Anzhiganov, 04/05/2025 03:43 PM
1 | 1 | Viacheslav Anzhiganov | # Group by day week or month |
---|---|---|---|
2 | |||
3 | Sum counts of an occurrence into days, weeks, or months |
||
4 | |||
5 | ## Group by day |
||
6 | |||
7 | ```sql |
||
8 | SELECT date_trunc('day', loggedin) AS "Day" , count(*) AS "No. of users" |
||
9 | FROM logins |
||
10 | WHERE created > now() - interval '3 months' |
||
11 | GROUP BY 1 |
||
12 | ORDER BY 1; |
||
13 | ``` |
||
14 | |||
15 | ## Group by week |
||
16 | |||
17 | ```sql |
||
18 | SELECT date_trunc('week', loggedin) AS "Week" , count(*) AS "No. of users" |
||
19 | FROM logins |
||
20 | WHERE created > now() - interval '3 months' |
||
21 | GROUP BY 1 |
||
22 | ORDER BY 1; |
||
23 | ``` |
||
24 | |||
25 | ## Group by month |
||
26 | |||
27 | ```sql |
||
28 | SELECT date_trunc('month', loggedin) AS "Month" , count(*) AS "No. of users" |
||
29 | FROM logins |
||
30 | WHERE created > now() - interval '1 year' |
||
31 | GROUP BY 1 |
||
32 | ORDER BY 1; |
||
33 | ``` |