2014-09-23 74 views
0

我想運行一個查詢像生成日期範圍天行(非連續)

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 
+0

爲什麼不這樣做:'select distinct start_date form NAEDO'? – 2014-09-23 12:53:33

+0

你如何提取一天? – morne 2014-09-23 12:54:07

+2

摘錄(從start_date開始的日期) – hbn 2014-09-23 12:56:15

回答

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 
+0

但它返回一個時間戳。這是postgres的標準嗎? – morne 2014-09-23 13:00:53

+1

@mornenel:你可能想'提取(從start_date開始的日期)'返回一個整數。 – 2014-09-23 13:12:03

+0

@a_horse_with_no_name,謝謝 – morne 2014-09-23 13:17:46

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 

也許還有一個辦法做到這一點沒有額外列...
但是,這是一個報告,你可以只在報告中省略額外的列。