2016-03-29 21 views
0

舉個例子,開始日期'2016-01-01'和結束日期'2016-06-01',天數'10th'是5,因爲它在1月到5月期間每個月都會出現。 「29日」的天數也是5,因爲這是閏年。 「30日」的天數爲4天,「31日」的天數爲3天,因爲它們只出現在某些月份。什麼是最簡單的方法來計算postgresql之間相同日期之間的天數

給定起始日期和結束日期的最簡單(或說最優雅)的方法來計算這個數字是什麼?

回答

0

您可以使用generate_series()日期列表,然後計算每一天的事件:

select extract(day from dt) as day_in_month, count(*) 
from generate_series(date '2016-01-01', date '2016-06-01', interval '1' day) as g(dt) 
group by extract(day from dt) 
order by 1; 
+0

酷,至少它的作品。對不起還不能投票給你。 –