【发布时间】:2020-10-01 23:18:47
【问题描述】:
我不经常编写原始 SQL,但最近在这样做时,我不禁认为这个查询不是很干燥。有办法收紧吗?
SELECT
COUNT(*) as "Users", DATE_TRUNC('day', created_at) AS "Date"
FROM
users
WHERE created_at > now() - interval '1 year'
GROUP BY DATE_TRUNC('day', created_at)
ORDER BY DATE_TRUNC('day', created_at);
目前返回:
Users Date
175 2019-10-01 00:00:00
54 2019-10-02 00:00:00
142 2019-10-03 00:00:00
...
我想要的是按created_at 日期对所有新用户进行分组,但只能追溯到一年(或我们选择的任意日期)。不确定DATE_TRUNC 是解决此问题的最佳方法。更确定重复3次可能不是。
【问题讨论】:
标签: sql postgresql datetime count sql-order-by