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

SQL累减语句

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

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

未能加载文件或程序集“file:///C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports

首先我使用工具的信息为:Visual Studio 2010 ,水晶报表的版本为:13.0.0.99。 我想做的就是:使用VS2010中的WPF,将DataSet中的数据展示出来。(DataSet中的数据来自Oracle数据库) 但是我在运行的时候出现了这个问题: 未能加载文件或程序集“...

HP Laserjet 1020 在Mac OSX下的安装

HP Laserjet 1020 在Mac OSX下的安装

这个教程是在Mac OSX 10.8操作系统下实现的,但是升级到10.9后,依旧能正常使用。 公司的打印机是HP Laserjet 1020,打印服务器和其他同事的电脑都是Windows系统,这个驱动还是很容易搞定的。 现在有一台Mac Mini,需要连打印机,但是官方不支持Mac OSX。在惠普网...

金蝶K/3修改帐套启用期间

不要乱修改 select * from t_SystemProfile where fkey = 'startyear' select * from t_SystemProfile where fkey = 'startperiod' 如果一个帐套已使用,要再修改会计期间个数或启用期间或会计期间开始...

PlatformIO串口无输出

PlatformIO串口无输出

问题:同样的串口打印代码,在Arduino IDE里可以正常输出,但是在PlatformIO里看不到任何输出硬件:ESP32-S3 Camera解决方法:This is because DTR and RTS both are connected to the RESET pin and GPIO...

Multisim汉化

Multisim汉化

对于中国人来说,还是喜欢用汉化版的。Multisim也能从网上下载到汉化包。汉化包的使用方法如下: 1、将汉化包拷贝到stringfiiles目录,如图所示:  2、打开Multisim,选【Option】->【Global Preferences】 3、在弹出的Preferen...

REG51.H详解

/* BYTE Register */ sfr P0 = 0x80; //P0口 sfr P1 = 0x90; //P1口 sfr P2 = 0xA0; //P2口 sfr P3 = 0xB0; //P3口 sfr PSW = 0xD0; //程序状态字,具体位意义见位定义 sfr ACC = 0x...

发表评论

访客

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