当前位置:首页 > 技术心得 > 正文内容

SQL累减语句

xjtudll4年前 (2022-02-07)技术心得7840

https://bbs.csdn.net/topics/70362619

表一
Item Qty
A 10
A 12
A 14
B 13
B 60
C 30
C 40
C 10
表二
Item Qty
A 1000
B 70
C 100
表三
Item Qty Result
A 10 990 =(1000-10)
A 12 978 =(990-12)
A 14 964 =(978-14)
B 13 57 =(70-13)
B 60 -3 =(57-60)
C 30 70 =(100-30)
C 40 30 =(70-40)
C 10 20 =(30-10)

--示例
--示例数据
create table 表一(Item varchar(10),Qty int)
insert 表一 select 'A',10
union all select 'A',12
union all select 'A',14
union all select 'B',13
union all select 'B',60
union all select 'C',30
union all select 'C',40
union all select 'C',10
create table 表二(Item varchar(10),Qty int)
insert 表二 select 'A',1000
union all select 'B',70
union all select 'C',100
go
--查询
select id=identity(int),a.*,Result=b.Qty
into # from 表一 a,表二 b
where a.Item=b.Item
order by a.Item
declare @Item varchar(10),@s int
update # set
@s=case when @Item=Item then @s-Qty else Result-Qty end,
@Item=Item,Result=@s
select * from #
drop table #
go
--删除测试
drop table 表一,表二
/*--结果
Item Qty Result
---------- ----------- -----------
A 10 990
A 12 978
A 14 964
B 13 57
B 60 -3
C 30 70
C 40 30
C 10 20
(所影响的行数为 8 行)
--*/

--示例
--示例数据
create table 表一(Item varchar(10),Qty int)
insert 表一 select 'A',10
union all select 'A',12
union all select 'A',14
union all select 'B',13
union all select 'B',60
union all select 'C',30
union all select 'C',40
union all select 'C',10
create table 表二(Item varchar(10),Qty int)
insert 表二 select 'A',1000
union all select 'B',70
union all select 'C',100
go
--查询
select id=identity(int),* into # from 表一
select a.Item,a.Qty,Result=b.Qty-(select sum(Qty) from # where Item=a.Item and id<=a.id)
from # a,表二 b
where a.Item=b.Item
drop table #
go
--删除测试
drop table 表一,表二
/*--结果
Item Qty Result
---------- ----------- -----------
A 10 990
A 12 978
A 14 964
B 13 57
B 60 -3
C 30 70
C 40 30
C 10 20
(所影响的行数为 8 行)
--*/

扫描二维码推送至手机访问。

版权声明:本文由鸟的天空发布,如需转载请注明出处。

本文链接:http://xjtudll.cn/Exp/632/

标签: SQL
分享给朋友:

“SQL累减语句” 的相关文章

proteus仿真时虚拟串口窗口丢失的解决办法

proteus仿真时虚拟串口窗口丢失的解决办法

在使用Proteus调试串口时会遇到自带的串口工具无法弹出虚拟串口解决办法如下:在Simulation中止后,进入工具栏Debug-> Reset Debug Pop up Windows,并在弹出窗口中选“yes”。 再次运行Simulation,可以发现窗口可以正常弹出。...

黑群晖:certificate has expired or is not yet valid

https://blog.csdn.net/weixin_54655073/article/details/138663733 sudo -imv /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt.baksud...

提交到app store时,提示ERROR ITMS-90478 ,ERROR ITMS-90062错误

提交到app store时,提示ERROR ITMS-90478 ,ERROR ITMS-90062错误

ios app提交到AppStore时,提示ERROR ITMS-90478 ,ERROR ITMS-90062,如下图所示: 看字面意思,应该是我提交的版本比之前上架的版本要低。 但是,之前的版本是V1.03,现在是V1.1.0。好像版本要高,并没有低。 然而,到AppStore构建版本那里查看...

Xcode:failed to get the task for process XXX 解决办法

问题:     iOS真机调试程序,报如下错误信息:failed to get the task for process XXX 原因: 证书问题,project和target的证书都必须是开发证书,不能用分发证书。 解决方案:     p...

NSTableview从mutable array获取数据

参考资料: http://stackoverflow.com/questions/3397952/populating-nstableview-from-a-mutable-array 可以使用以下的方法 – numberOfRowsInTableView:...

电路板上的“黑疙瘩”,里面究竟有什么?

电路板上的“黑疙瘩”,里面究竟有什么?

现在很多日用电子产品都非常便宜,比如计算器、遥控器之类的,它们实在太便宜了,以至于成本控制的过程不允许让生产厂商将每一片芯片都封装好,于是“牛屎片”便产生了。 它的学名叫做COB(Chip On Board),你一定在很多便宜的电子产品中见到它。这种封装形式采用黑色的树脂将...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。