[英]How can I change the value for “WHERE startdate >= date” clause and get data for a range of dates?
我正在查詢如下所示的property_table,顯示該屬性在哪個日期到哪個日期處於活動狀態。
fid startdate enddate
--- ---------- --------
2588727 2019-01-11 2019-05-09
2591038 2019-01-11 2019-01-18
2587420 2019-03-11 2019-04-09
2592269 2019-03-29 2019-03-09
fid
是房屋/公寓的flat_id。
我正在嘗試以以下格式2019-03-15
至2019-04-30
之間每天有多少個公寓活躍。
Date active_count
---------- -----------
2019-03-15 235631
2019-03-16 234545
2019-03-17 234334
2019-03-29 342123
..
..
2019-04-30 344322
我可以使用以下查詢在一天中找到活躍的公寓。 請幫助查找上述整個日期范圍。
-- Number of flats on '2019-03-20' Date
SELECT COUNT(*)
FROM invent_table
WHERE startdate <= '2019-03-20'
AND enddate >= '2019-03-20'
使用generate_series()
您可以創建感興趣的日子的系列,加入表並分組:
select d.Date, count(i.fid) active_count
from (
select generate_series(date '2019-03-15', date '2019-04-30', '1 day') as Date
) d left join invent_table i
on d.Date between i.startdate and i.enddate
group by d.Date
order by d.Date
參見演示 。
您是否正在尋找這樣的東西?
SELECT COUNT(*)
FROM invent_table
WHERE startdate >= '2019-03-15'
AND enddate <= '2019-04-30'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.