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

SQL累减语句

xjtudll5年前 (2022-02-07)技术心得10110

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://www.xjtudll.cn/Exp/632/

标签: SQL
分享给朋友:

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

C语言交换两个数

可能大家都认为交换两个数如此的简单,没必要写,没新意。例如,交换a和b两个数,教科书上一直这样写: temp=b; a=temp; b=a; 确实很简单。但是最近看到一段代码,觉得十分高超,是这样的: a^=b; b^=a; a^=b; 同样也是三句代码,但是却未...

itunes无法备份ipad,因为未能与ipad开始会话

现象:ipad连接电脑后,提示“itunes无法备份ipad,因为未能与ipad开始会话” 原因:之前备份的时候发现备份进度条长时间未动,以为是死机,所以拔掉了ipad,重新连接后,无法备份。 解决办法: 1、在itunes偏好设置里,删除ipad的旧有备份 2、修复系统权限(不知道这步是不...

Proteus运行找不到sdf文件解决方法

Proteus运行找不到sdf文件解决方法

很多人在用Proteus的时候,Proteus会提示找不到sdf文件,如图所示。提示Cannot open …… .sdf。这种情况经常出现在电子类软件,主要是软件仿真过程中使用的文件不能在有中文路径的文件夹里。用Mulitism的时候也会出现这个问题的。这个原因很简单,...

金蝶K3实际成本各种计算方法的区别及举例

金蝶K3实际成本各种计算方法的区别及举例

http://blog.sina.cn/dpool/blog/s/blog_166bf582c0102wzcl.html 实际成本各种计算方法的区别及举例 K3WISE实际成本包含四种计算方法,分别是品种法、分步法、分批法和分类法。品种法可视同为分步法的一种特殊情形,即没有中间产品,材料一步...

PDF factory pro Server Edition注册教程

PDF factory pro Server Edition注册教程

问题: 有注册码,但是找不到输入注册码的地方 版本:pdf Factory pro Server Edition 5.2.5 环境: Windows server 2008 R2 网上说试着打印一个文件,就会弹出注册窗口,实际没用。这个方法估计是针对单机版,不是服务器版的。 解决办法: 1、打开控...

Warning: xx pins must meet Altera requirements for 3.3-, 3.0-, and 2.5-V interfaces. For more information, refer to AN 447: Interfacing Cyclone IV E Devices with 3.3/3.0/2.5-V LVTTL/LVCMOS I/O Systems

Warning: xx pins must meet Altera requirements for 3.3-, 3.0-, and 2.5-V interfaces. For more information, refer to AN 447: Interfacing Cyclone IV E Devices with 3.3/3.0/2.5-V LVTTL/LVCMOS I/O Systems

问题描述: Cyclone IV EP4CE6E22C8N Quartus II编译 每次编译完,都有个warning: Warning: xx pins must meet Altera requirements for 3.3-, 3.0-, and 2.5-V interfaces. For...

发表评论

访客

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