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

SQL累减语句

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

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

Xcode5如何添加或更换图标

Xcode5如何添加或更换图标

参考文献: 官方教程:https://developer.apple.com/library/ios/qa/qa1686/_index.html 1、添加图标图片资源到工程 2、选择对应的App Icons。如果从来没有添加过,【Resource】处可以自己选择想要的图标资源。选择好后编译即可。编...

后缀名为.Q的文件为什么用按键精灵无法打开

后缀名为.Q的文件为什么用按键精灵无法打开

问题:.Q文件无法用按键精灵直接打开 解决办法: 将文件放在按键精灵目录 QMScript文件夹里。然后在按键精灵上刷新就有了 以“我的脚本.Q”为例,...

IIS HTTP 500错误解决

操作系统:Windows XP SP3 IIS:6.0 IIS安装完成,一运行出现“HTTP 500 - 内部服务器错误”,网上找了一圈,处理方法那个叫复杂,在研究完复杂方法之后,发现只要三步就可以解决问题了: 1. 运行:regsvr32 %windir%/syste...

获取NSImage图片的准确大小

问题: NSImage加载图片的时候无法准确获得图片的大小 测试代码: NSImage *image = [NSImage imageNamed:@"image"]; NSLog(@"%f, %f", image.size.width, image.size....

eclipse:android R.java文件丢失或无法更新

eclipse:android R.java文件丢失或无法更新

http://www.cnblogs.com/zdz8207/archive/2012/11/30/eclipse-android-adt-update.html 问题: eclipse编译android工程,发现R.java无法更新 解决办法: 1、先确定SDK没问题,就是说工程设置里用的SDK,...

Eclipse导入Android工程,出现default与Displaying的问题解决

Eclipse导入android工程时出现如下提示: [15:04:03 - XXX] 'default' is not a best match for any device/locale combination.  [ 15:04:03 - XXX] Displaying it wi...

发表评论

访客

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