top

笔记206 查询销量、库存的查询汇总语句不用group by

笔记206 查询销量、库存的查询汇总语句不用group by

复制代码
 1 --查询销量、库存的查询汇总语句不用group by
 2 --> 测试数据:@Product
 3 declare @Product table([PID] int,[Name] varchar(7))
 4 insert @Product
 5 select 1,'Nugget' union all
 6 select 2,'Seafood'
 7 
 8 --> 测试数据:@Buy
 9 declare @Buy table([Date] varchar(3),[PID] int,[Qty] int)
10 insert @Buy
11 select '1/7',1,10 union all
12 select '2/7',2,20 union all
13 select '3/7',1,10
14 
15 --> 测试数据:@Sale
16 declare @Sale table([Date] varchar(3),[PID] int,[Qty] int)
17 insert @Sale
18 select '5/7',1,2 union all
19 select '5/7',2,3 union all
20 select '6/7',1,4 union all
21 select '6/7',2,5
22 
23 select [Name],
24      (select sum(Qty) from @Buy where PID=t.PID) as  BuyQty,
25      (select sum(Qty) from @Sale where PID=t.PID) as  SaleQty,
26      (select sum(Qty) from @Buy where PID=t.PID)-
27      (select sum(Qty) from @Sale where PID=t.PID) as Balance
28 from @Product t
复制代码

 

posted @   桦仔  阅读(370)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示