在Postgres的GROUP BY日

时间:2014-06-24 14:21:07

标签: postgresql

我是Postgres的新手。我有一张看起来像这样的表:

User
----
id (int)
createdOn (bigint)
isDeleted (boolean)

createdOn表示自EPOCH以来的毫秒数。我试图找出每天创建了多少用户,其中isDeleted标志为0.我已经尝试了以下内容,但它不起作用:

SELECT date_trunc('day', u.createdOn) AS "Day" , count(*) AS "No. of users"
FROM User u
WHERE u.isDeleted = 0 
GROUP BY 1 
ORDER BY 1;

如何创建此查询?

谢谢!

1 个答案:

答案 0 :(得分:2)

你没有说什么不起作用,但我想这是date_trunc函数

select
    date_trunc('day', to_timestamp(createdOn)) as "Day",
    count(*) as "No. of users"
from User
where isDeleted = 0
group by 1
order by 1;