我有一张表,我想为每一行查找总数中剩余的金额。但是,金额的顺序是按升序排列的。
id amount
1 3
2 2
3 1
4 5
结果应如下所示:
id remainder
1 10
2 8
3 5
4 0
关于如何做到这一点的任何想法?我猜over子句是要走的路,但我不能把它拼凑起来。谢谢。
我有一张表,我想为每一行查找总数中剩余的金额。但是,金额的顺序是按升序排列的。
id amount
1 3
2 2
3 1
4 5
结果应如下所示:
id remainder
1 10
2 8
3 5
4 0
关于如何做到这一点的任何想法?我猜over子句是要走的路,但我不能把它拼凑起来。谢谢。
由于您没有指定您的 RDBMS,我将假设它是 Postgresql ;-)
select *, sum(amount) over() - sum(amount) over(order by amount) as remainder
from tbl;
输出:
| ID | AMOUNT | REMAINDER |
---------------------------
| 3 | 1 | 10 |
| 2 | 2 | 8 |
| 1 | 3 | 5 |
| 4 | 5 | 0 |
它是如何工作的:http ://www.sqlfiddle.com/#!1/c446a/5
它也适用于 SQL Server 2012:http ://www.sqlfiddle.com/#!6/c446a/1
SQL Server 2008 解决方案的思考...
顺便说一句,您的 ID 只是一个行号吗?如果是,请执行以下操作:
select
row_number() over(order by amount) as rn
, sum(amount) over() - sum(amount) over(order by amount) as remainder
from tbl
order by rn;
输出:
| RN | REMAINDER |
------------------
| 1 | 10 |
| 2 | 8 |
| 3 | 5 |
| 4 | 0 |
但是,如果您确实需要完整的 ID 并在顶部移动最少的数量,请执行以下操作:
with a as
(
select *, sum(amount) over() - sum(amount) over(order by amount) as remainder,
row_number() over(order by id) as id_sort,
row_number() over(order by amount) as amount_sort
from tbl
)
select a.id, sort.remainder
from a
join a sort on sort.amount_sort = a.id_sort
order by a.id_sort;
输出:
| ID | REMAINDER |
------------------
| 1 | 10 |
| 2 | 8 |
| 3 | 5 |
| 4 | 0 |
在此处查看查询进度:http ://www.sqlfiddle.com/#!6/c446a/11
我只想提供一种更简单的方法来按降序执行此操作:
select id, sum(amount) over (order by id desc) as Remainder
from t
这将适用于 Oracle、SQL Server 2012 和 Postgres。
一般的解决方案需要一个自连接:
select t.id, coalesce(sum(tafter.amount), 0) as Remainder
from t left outer join
t tafter
on t.id < tafter.id
group by t.id
SQL Server 2008 回答,我无法提供 SQL Fiddle,似乎它剥离了begin
关键字,导致语法错误。我在我的机器上测试了这个:
create function RunningTotalGuarded()
returns @ReturnTable table(
Id int,
Amount int not null,
RunningTotal int not null,
RN int identity(1,1) not null primary key clustered
)
as
begin
insert into @ReturnTable(id, amount, RunningTotal)
select id, amount, 0 from tbl order by amount;
declare @RunningTotal numeric(16,4) = 0;
declare @rn_check int = 0;
update @ReturnTable
set
@rn_check = @rn_check + 1
,@RunningTotal =
case when rn = @rn_check then
@RunningTotal + Amount
else
1 / 0
end
,RunningTotal = @RunningTotal;
return;
end;
为了达到你想要的输出:
with a as
(
select *, sum(amount) over() - RunningTotal as remainder
, row_number() over(order by id) as id_order
from RunningTotalGuarded()
)
select a.id, amount_order.remainder
from a
inner join a amount_order on amount_order.rn = a.id_order;
保护运行总数的基本原理:http ://www.ienablemuch.com/2012/05/recursive-cte-is-evil-and-cursor-is.html
选择较小的邪恶;-)