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

SQL累减语句

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

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累减语句” 的相关文章

Xcode Warning: Multiple build commands for output file /xxx 解决

Xcode Warning: Multiple build commands for output file /xxx 解决

 现象: 编译后有如下警告: [WARN]Warning: Multiple build commands for output file /xxx 解决步骤如下: 1、选择你的工程 2、选择target 3、点击 Build Phases 4、展开...

金蝶K3 怎么实现一个物品 对应多个供应商

金蝶K3 怎么实现一个物品 对应多个供应商

在基础资料里面的采购管理这一块,可以设定每个供应商供应的产品,而且每个供应商的产品可以重复。 虽然会有提示,确定就是了 没问题的...

ios宏: 判断是否是retina屏,是否iPad,设备判断(4,5,5s,6等)

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) #def...

把存储过程结果集插入临时表

--使用sp_helpserver 显示可用的服务器。  exec sp_helpserver  --打开服务器的"data access"属性  exec sp_serveroption '服务器名',...

12点是AM还是PM

12点是AM还是PM

中午12点是AM还是PM,有很多人会搞混。有说是AM的,也有说是PM的。 先说说AM和PM的含义吧 AM:ante meridiem(来自拉丁文) PM:post meridiem(来自拉丁文) 那么12点究竟是AM还是PM呢?一般习惯说中午十二点为12 PM, 深夜十二点为12 AM,正式用法是&...

IIS+PHP环境下文件上传没法访问问题

PHP运行环境:IIS FastCGI 在IIS FastCGI方式运行时,经过浏览器请求的Web资源有2类,一个是经由php_cgi.exe产生的内容,一部分是静态资源,由IIS返回给用户。 这2类用户有区别,前者是IIS_IUSERS,后者是Authenticated User或者是Anonym...

发表评论

访客

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