我有一张包含不同时间段价格的表格和一张包含标准价格的表格:
rates:
user_id | start | end | price | charges
1 | 2023-11-13 | 2023-11-19 | 100 | 25
1 | 2023-11-21 | 2023-11-23 | 120 | 30
rates_standard:
user_id | price | charges
1 | 75 | 20
SELECT
SUM(
DateDiff(
Least(r.end + INTERVAL 1 DAY, '2023-11-24'),
Greatest(r.start, '2023-11-10')
) * r.price
) price
FROM
rates r
WHERE
r.end >= '2023-11-10' AND
r.start < '2023-11-24'
通过此查询,我得到的总价为 1060
7 x 100 +
3 x 120
= 1060
查询日期若非季节,则以标准价格为准。 我还想添加一次开始日期的费用。如果开始日期不在一个赛季内,则应使用标准收费。 非常感谢您的帮助!
假设已发布的代码按预期工作,然后扩展费率以填充第一行之前和最后一行之后的扩展,并包括没有费率的 rates_basic,这就是我的做法..
SET @START = '2023-11-13';
SET @end = '2023-11-13';
set @uid = 1;
with cte as
(
select 0 as src,user_id,'1957-01-01' seasonstart, '1957-01-01' seasonend, price from rates_basic
union
select 10 as src,user_id,cast(@start as date) ss,
date_sub(@minstart, interval 1 day) se,
(select price from rates_basic rb where rb.user_id = r.user_id) price
from rates r
where @start < (select min(seasonstart) from rates where user_id = @uid)
union
select 20 as src,user_id,seasonstart,seasonend, price from rates
union
select 25 as src,user_id,
date_add(seasonend,interval 1 day) seasonstar,
date_sub(lead(seasonstart) over (partition by user_id order by seasonstart),interval 1 day) seasonend,
(select price from rates_basic rb where rb.user_id = r.user_id) price
from rates r
union
select 30 as src,user_id,date_add(@maxend,interval 1 day) seasonstart,
@end seasonend,
(select price from rates_basic rb where rb.user_id = r.user_id) price
from rates r
where @end > (select max(seasonend) from rates where user_id = @uid)
)
#select * from cte where src <> 0 and seasonend is not null
SELECT
SUM(
DateDiff(
Least(r.seasonend + INTERVAL 1 DAY, @end),
Greatest(r.seasonstart, @start)
) * r.price
) price
FROM
cte r
WHERE
r.seasonend >= @start AND
r.seasonstart < @end and
src <> 0 and r.seasonend is not null;
我正在使用用户定义的 var1ables,因为我不想在代码中重新输入。您可能希望创建一个函数,将这些作为参数传递以达到您的目的。 src 的值可以帮助调试。