美文网首页
postgresql按日、周、月分组

postgresql按日、周、月分组

作者: 渣渣曦 | 来源:发表于2019-11-15 10:50 被阅读0次

按天分组

SELECT date_trunc('day', loggedin) AS "Day" , count(*) AS "No. of users"
FROM logins
WHERE created > now() - interval '3 months' 
GROUP BY 1 
ORDER BY 1;

按周分组

SELECT date_trunc('week', loggedin) AS "Week" , count(*) AS "No. of users"
FROM logins
WHERE created > now() - interval '3 months' 
GROUP BY 1
ORDER BY 1;

按月分组

SELECT date_trunc('month', loggedin) AS "Month" , count(*) AS "No. of users"
FROM logins
WHERE created > now() - interval '1 year' 
GROUP BY 1
ORDER BY 1;

显示近十个月年月

SELECT to_char(date_trunc('month', YearMonth),'YYYY-MM')
FROM generate_series(current_date - interval '10 month',
        current_date, '1 month'::interval) YearMonth;

相关文章

网友评论

      本文标题:postgresql按日、周、月分组

      本文链接:https://www.haomeiwen.com/subject/kimjictx.html