将结果行更改为周SQL

时间:2013-11-22 05:32:20

标签: sql postgresql

我有当前的查询

select (current_timestamp - dstat.updated_at) as days_since_activation
from trips

返回

days_since_activation
  77 days, 9:41:23.764788

我如何将其变成几周而不是几天?

1 个答案:

答案 0 :(得分:0)

尝试:

SELECT 
(EXTRACT(WEEK FROM current_timestamp) - EXTRACT(WEEK FROM updated_at)) 
AS weeks_since_activation
FROM trips

或者:

SELECT FLOOR(date_part('days',(current_timestamp - updated_at)) / 7) 
AS weeks_since_activation
FROM trips;

请参阅Fiddle Demo