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

SQL累减语句

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

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 行)
--*/

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

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

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

标签: SQL
分享给朋友:

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

C#中实现VB中的CreateObject方法

经常看到有些VB的例子中直接用个CreateObject就可调用系统功能(大多是COM对象),像用户设定,网络设定等等。虽然C#中可以通过使用VB的命名空间的方法来调用CreateObject函数,但是这样比较没什么用,因为生成的对象的所带有的方法都不能使用。C#中还可以直接用添加引用的方式来调用一...

宏与子程序的区别

宏和子程序都是为了简化源程序的编写,提高程序的可维护性,但是它们二者之间存在着以下本质的区别:1、在源程序中,通过书写宏名来引用宏,而子程序是通过CALL指令来调用;2、汇编程序对宏通过宏扩展来加入其定义体,宏引用多少次,就相应扩展多少次,所以,引用宏不会缩短目标程序;而子程序代码在目标程序中只出现...

ESD保护方法

ESD保护方法

为了给电子系统提供ESD保护,可以从不同的角度来着手。 一种方法是在半导体芯片内建ESD保护架构。不过,日趋缩小的CMOS芯片已经越来越不足以承受进行内部2 kV等级的ESD保护所需要的面积。 其次,也可以在物理电路设计方面下功夫,较敏感的电路元件应该尽量远离通孔或接缝处,如果可能的话,线缆连接...

TM89系列单片机使用注意事项

TM89系列单片机使用注意事项

一、大电流模式大电流模式,官方资料上叫“电力备援模式”(详见UM-TM89XXMCUfunction),Back Up Mode。当系统耗电较大时,有可能使得系统的电源电压产生很大的波动,有可能导致MCU不正常动作,为了解决这个问题,在耗电较大时,需要开启大电流模式。 3V供电时,当选用BCF=0时...

群晖Apache 忽略文件名大小写

群晖Apache 忽略文件名大小写

参考资料:https://www.cnblogs.com/shipment/p/14767848.html 1、SSH登录群晖ssh  admin@201.201.201.1682、修改conf文件权限为777sudo chmod 777 /volume1/@appstore/Apache...

金蝶K3:自定义SQL报表,如何在预警平台选择得到?

金蝶K3:自定义SQL报表,如何在预警平台选择得到?

自定义的SQL报表,在预警平台选择不到呢?是哪里设置问题呢?谢谢了 解决方法:BOS中将报表发布到信息服务就可以找到了。...

发表评论

访客

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