PostgreSQL:查询以查找空闲天数?
PostgreSQL: Query to find number of free days?
我有 table 如下所示。此 table 显示从 开始日期 到 结束期间用户将使用 free/available 的车辆日期.
假设
从 2018-01-15 到 2020-02-28 有车辆 id = 1 可供用户使用(格式为 yyyy-mm-dd)。在此期间任何用户都可以租用车辆。
我想要的:
我想计算特定时段的空闲天数。
此处时间段:2018 年 1 月表示(1-Jan-2018
至 31-Jan-2018
)。
免费天数的计算标准:
对于车辆 ID = 1 --> 开始日期 = 2018-01-15
和结束日期 = 2020-02-28
2018 年 1 月 = 16 天
(因为 1 月的总天数是 31,但我们的开始日期是从 2018-01-15
开始,车辆 ID =1)
2018 年 2 月 = 28 天(在 2018-01-15
到 2020-02-28
之间)
您可以对这两个日期使用减号运算符和日期范围数据类型。
postgres=# select *,upper(intersection_range)-lower(intersection_range) as available_days from (select *, daterange(start_date,end_date) * daterange('2018-01-01','2018-01-31') as intersection_range from vehicle) a;
vehicle_id | start_date | end_date | intersection_range | available_days
------------+------------+------------+-------------------------+----------------
1 | 2017-12-31 | 2020-02-28 | [2018-01-01,2018-01-31) | 30
2 | 2017-11-30 | 2018-02-28 | [2018-01-01,2018-01-31) | 30
3 | 2017-07-31 | 2019-02-28 | [2018-01-01,2018-01-31) | 30
(3 rows)
此致。
Select enddate, startdate, CASE
WHEN startdate<= '2017-01-01' and enddate>= '2017-01-31' THEN ('2017-01-31'::date - '2017-01-01'::date)+1
WHEN startdate<= '2017-01-01' and enddate< '2017-01-31' and enddate>= '2017-01-01' THEN (enddate::date - '2017-01-01') +1
WHEN startdate> '2017-01-01' and enddate>= '2017-01-31' and startdate<= '2017-01-31' THEN (('2017-01-31'::date - '2017-01-01'::date)+1) - EXTRACT(DAY FROM startdate::date))+1
WHEN startdate> '2017-01-01' and enddate< '2017-01-31' and startdate<= '2017-01-31' and enddate>= '2017-01-01' THEN (enddate::date - startdate::date)+1
end
as td from table1
我有 table 如下所示。此 table 显示从 开始日期 到 结束期间用户将使用 free/available 的车辆日期.
假设
从 2018-01-15 到 2020-02-28 有车辆 id = 1 可供用户使用(格式为 yyyy-mm-dd)。在此期间任何用户都可以租用车辆。
我想要的:
我想计算特定时段的空闲天数。
此处时间段:2018 年 1 月表示(1-Jan-2018
至 31-Jan-2018
)。
免费天数的计算标准:
对于车辆 ID = 1 --> 开始日期 = 2018-01-15
和结束日期 = 2020-02-28
2018 年 1 月 = 16 天
(因为 1 月的总天数是 31,但我们的开始日期是从 2018-01-15
开始,车辆 ID =1)
2018 年 2 月 = 28 天(在 2018-01-15
到 2020-02-28
之间)
您可以对这两个日期使用减号运算符和日期范围数据类型。
postgres=# select *,upper(intersection_range)-lower(intersection_range) as available_days from (select *, daterange(start_date,end_date) * daterange('2018-01-01','2018-01-31') as intersection_range from vehicle) a;
vehicle_id | start_date | end_date | intersection_range | available_days
------------+------------+------------+-------------------------+----------------
1 | 2017-12-31 | 2020-02-28 | [2018-01-01,2018-01-31) | 30
2 | 2017-11-30 | 2018-02-28 | [2018-01-01,2018-01-31) | 30
3 | 2017-07-31 | 2019-02-28 | [2018-01-01,2018-01-31) | 30
(3 rows)
此致。
Select enddate, startdate, CASE
WHEN startdate<= '2017-01-01' and enddate>= '2017-01-31' THEN ('2017-01-31'::date - '2017-01-01'::date)+1
WHEN startdate<= '2017-01-01' and enddate< '2017-01-31' and enddate>= '2017-01-01' THEN (enddate::date - '2017-01-01') +1
WHEN startdate> '2017-01-01' and enddate>= '2017-01-31' and startdate<= '2017-01-31' THEN (('2017-01-31'::date - '2017-01-01'::date)+1) - EXTRACT(DAY FROM startdate::date))+1
WHEN startdate> '2017-01-01' and enddate< '2017-01-31' and startdate<= '2017-01-31' and enddate>= '2017-01-01' THEN (enddate::date - startdate::date)+1
end
as td from table1