[英]How can I change the value for “WHERE startdate >= date” clause and get data for a range of dates?
I am querying a property_table that looks like below, showing between which date to which date the property were active. 我正在查询如下所示的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
is the flat_id for a house/flat. fid
是房屋/公寓的flat_id。
I am trying to find out how many flats were active on each day between 2019-03-15
and 2019-04-30
in the below format. 我正在尝试以以下格式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
I am able to find active flats on a single day by using the below query. 我可以使用以下查询在一天中找到活跃的公寓。 Please help to find for the entire date range as above. 请帮助查找上述整个日期范围。
-- Number of flats on '2019-03-20' Date
SELECT COUNT(*)
FROM invent_table
WHERE startdate <= '2019-03-20'
AND enddate >= '2019-03-20'
With generate_series()
you create the series of the days you are interested in, join to the table and group: 使用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
Are your looking for something like this? 您是否正在寻找这样的东西?
SELECT COUNT(*)
FROM invent_table
WHERE startdate >= '2019-03-15'
AND enddate <= '2019-04-30'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.