简体   繁体   English

日期范围之间的总价(例如酒店预订季节)

[英]Total Price between date range (like hotel reservation seasons)

 id       carID     date1         date2      price
 ---      ----     ---------    ---------    -----
 1        1       1000-01-01    9999-12-31   100

 2        2       1000-01-01    2014-06-06   100 
 3        2       2014-06-07    2014-06-07   150
 4        2       2014-06-08    9999-12-31   100

 5        3       1000-01-01    2014-06-01   150
 6        3       2014-06-02    2014-07-01   200
 7        3       2014-07-02    2014-07-25   300
 8        3       2014-07-26    9999-12-31   100 

This is my price table; 这是我的价格表; I want to calculate total price with my dates range. 我想用我的日期范围计算总价。

I want to display all cars with total price; 我想显示所有价格合计的汽车;

for example my filter dates: 例如我的过滤日期:

date1: 2014-06-01 date1: 2014-06-01
date2: 2014-06-22 date2: 2014-06-22

RESULTS 结果

CARID    TOTAL
------ ------
1      2100
2      2150
3      4150

And I try this , but it is only for one carID. 我尝试了此方法,但这仅适用于一个carID。
calculate price between given dates 计算给定日期之间的价格
Also This; 也这个;
calculate price between given dates in multiple ranges 计算多个日期范围内给定日期之间的价格

Assuming you have variables @date1 and @date2 , you can do this with conditional aggregation: 假设您有变量@date1@date2 ,则可以通过条件聚合来实现:

select carid,
       sum(1 + greatest(least(date2, @date2) - greatest(date1, @date1), 0) * price)
from table t
group by carid;

This just replaces the boundaries of each period in the table with the intersection with the outer boundaries. 这只是将表中每个期间的边界替换为与外部边界的交集。 It then sums up the duration times the price. 然后,将持续时间乘以价格得出的总和。 The 1 + seems to handle the case that date2 appears to be a valid date for the price, rather then the first date when it is not active. 1 +似乎可以处理date2似乎是价格的有效日期,而不是无效的第一个日期的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM