如何在sql中获取结果集 [英] How to get result set in sql
本文介绍了如何在sql中获取结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的表
my table
vID Amt
2 50
2 60
4 120
4 110
5 50
5 70
如何获得结果
how to get result
vID Amt
2 10
4 10
5 20
推荐答案
试试这个..
try this..
select vID,max(isnull(amt,0))-min(isnull(amt,0)) as SubValue from [dbo].[My_Table] group by vID
检查一下,它有你的运行金额要求。
使用自连接 - 而不是光标或循环 [ ^ ]
而不是添加,写下减法逻辑。
你也可以这样做< br $>
check this, its kind of running sum requirement you have.
Use-Of-Self-Joins-Instead-Of-Cursor-Or-While-Loop[^]
Instead of adding , write the logic of subtraction.
Also you can do this
select vID , Amt, Row_Number() Over (Partition by Vid Order by Vid) As ROWID into #temp
from table
select b.Vid,B.Amt-A.amt
from
(select * from #temp Where ROWID=2 ) b
Join
(select * from #temp Where ROWID=1 ) a
On b.Vid=a.Vid
试试这个
Try this
select vid, max(Amt) - min(Amt) from table group by vid;
这篇关于如何在sql中获取结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文