从日期范围生成日期行(非连续)

时间:2014-09-23 12:52:27

标签: sql postgresql between date-range

我想运行像

这样的查询
SELECT distinct( generate_series(timestamp start_date, interval '1' day) as d
FROM NAEDO
WHERE start_date >= '2014/08/13'
AND start_date <= '2014/09/12'
ORDER BY start_date

并返回如下数据:(范围内的天数,但来自数据[可能连续几天])

days
---
13
16
17
18
25
29
1
3

2 个答案:

答案 0 :(得分:0)

这是你在找什么?

SELECT distinct date_trunc('day', start_date) as d
FROM NAEDO
WHERE start_date >= '2014-08-13' AND start_date <= '2014-09-12'
ORDER BY d

答案 1 :(得分:0)

SELECT  DISTINCT start_date, extract(day from start_date) as d
FROM NAEDO
WHERE start_date >= '2014-08-13' AND start_date <= '2014-09-12'
ORDER BY start_date

如果没有额外的列,也许还有办法可以做到这一点...... 但这是针对报告的,您可以省略报告中的额外列。