运行 减法,但仅在金额仍高于限制时减法
Running subtraction but subtracting only if the amount is still above a limit
我有一个具有以下结构的 table,我想将 TotalAmount 减去 Amount,但前提是剩余金额仍高于限制。排序是在继续下一个数据之前应该首先尝试的顺序
Sort Amount Limit TotalAmount
1 200 500 2000
3 500 500 2000
10 1000 500 2000
17 30 500 2000
预期结果
Sort Amount Limit TotalAmount RemainingAmount
1 200 500 2000 1800
3 500 500 2000 1300
10 1000 500 2000 1300 --This is still 1300 because 1300 - 1000 is 300
17 30 500 2000 1270
我一直在尝试 window 函数,但我不知道如何应用在条件下保持相同数量的逻辑。
SELECT Sort, Amount, Limit, TotalAmount, TotalAmount - SUM(Amount) OVER (ORDER BY Sort) as RemainingAmount
我也尝试了一些案例并检查我是否低于限制
CASE WHEN TotalAmount - SUM(Amount) OVER (ORDER BY Sort) < Limit THEN --readd the amount again?
您可以执行自我 join
生成基线 运行 总计,然后找到每个给定 sort
的最小 运行 总计,当减去sort
的金额不低于限制:
with aggs(s, a) as (
select v.sort, sum(coalesce(v1.amount, 0)) from vals v
left join vals v1 on v1.sort < v.sort
group by v.sort order by v.sort
),
r_m(s, a) as (
select v.sort, min(v.totalamount - a1.a) from vals v
join aggs a1 on v.totalamount - a1.a >= v.lmt and v.sort >= a1.s
group by v.sort
order by v.sort
)
select v.*, case when r_m.a - v.amount >= v.lmt
then r_m.a - v.amount else r_m.a end
from vals v join r_m on v.sort = r_m.s
输出:
sort
amount
lmt
totalamount
remainingamount
1
200
500
2000
1800
3
500
500
2000
1300
10
1000
500
2000
1300
17
30
500
2000
1270
声明一个 运行 变量 @Amount
以记住扣除的金额并对所选的每一行进行计算。
SELECT Sort, Amount, `Limit`, TotalAmount,
CASE WHEN TotalAmount - @Amount - Amount < `Limit`
THEN TotalAmount - @Amount ELSE TotalAmount - (@Amount := @Amount + Amount)
END AS RemainingAmount
FROM v
JOIN ( SELECT @Amount := 0 ) f
ORDER BY Sort
我有一个具有以下结构的 table,我想将 TotalAmount 减去 Amount,但前提是剩余金额仍高于限制。排序是在继续下一个数据之前应该首先尝试的顺序
Sort Amount Limit TotalAmount
1 200 500 2000
3 500 500 2000
10 1000 500 2000
17 30 500 2000
预期结果
Sort Amount Limit TotalAmount RemainingAmount
1 200 500 2000 1800
3 500 500 2000 1300
10 1000 500 2000 1300 --This is still 1300 because 1300 - 1000 is 300
17 30 500 2000 1270
我一直在尝试 window 函数,但我不知道如何应用在条件下保持相同数量的逻辑。
SELECT Sort, Amount, Limit, TotalAmount, TotalAmount - SUM(Amount) OVER (ORDER BY Sort) as RemainingAmount
我也尝试了一些案例并检查我是否低于限制
CASE WHEN TotalAmount - SUM(Amount) OVER (ORDER BY Sort) < Limit THEN --readd the amount again?
您可以执行自我 join
生成基线 运行 总计,然后找到每个给定 sort
的最小 运行 总计,当减去sort
的金额不低于限制:
with aggs(s, a) as (
select v.sort, sum(coalesce(v1.amount, 0)) from vals v
left join vals v1 on v1.sort < v.sort
group by v.sort order by v.sort
),
r_m(s, a) as (
select v.sort, min(v.totalamount - a1.a) from vals v
join aggs a1 on v.totalamount - a1.a >= v.lmt and v.sort >= a1.s
group by v.sort
order by v.sort
)
select v.*, case when r_m.a - v.amount >= v.lmt
then r_m.a - v.amount else r_m.a end
from vals v join r_m on v.sort = r_m.s
输出:
sort | amount | lmt | totalamount | remainingamount |
---|---|---|---|---|
1 | 200 | 500 | 2000 | 1800 |
3 | 500 | 500 | 2000 | 1300 |
10 | 1000 | 500 | 2000 | 1300 |
17 | 30 | 500 | 2000 | 1270 |
声明一个 运行 变量 @Amount
以记住扣除的金额并对所选的每一行进行计算。
SELECT Sort, Amount, `Limit`, TotalAmount,
CASE WHEN TotalAmount - @Amount - Amount < `Limit`
THEN TotalAmount - @Amount ELSE TotalAmount - (@Amount := @Amount + Amount)
END AS RemainingAmount
FROM v
JOIN ( SELECT @Amount := 0 ) f
ORDER BY Sort