计算SQL中日期差异的分布

时间:2014-07-24 10:33:46

标签: sql postgresql

在我们的数据库中,每个用户都有created_atcancelled_at日期。如何计算用户活动天数的分布(具有cancelled_at日期)。

3 个答案:

答案 0 :(得分:1)

通过分发,我希望你能看到直方图。为此,您需要聚合:

select date_part('day', cancelled_at - created_at) as activedays, count(*)
from databasetable
group by date_part('day', cancelled_at - created_at)
order by activedays;

答案 1 :(得分:0)

SELECT *,DATE_PART('day', cancelled_at - created_at) as daysActive
FROM tableName
WHERE cancelled_at IS NOT NULL

答案 2 :(得分:0)

with cte as
(
SELECT DATE_PART ('day', cancelled_at - created_at) as daysactive FROM yourTable WHERE
cancelled_at
IS NOT NULL 
)
select daysactive || ' day(s)'  as days_active from cte