列和相减 L3-L2
--列和相减 L3-L2
declare @test table(L1 varchar(50),L2 int,L3 int)
insert into @test
select 'A1',10,10 union all
select 'A1',10,5 union all
select 'A1',5,5 union all
select 'B1',10,20 union all
select 'B1',5,5
select * from @test
select a.*,b.L4 from @test a join
(select L1,sum(L3)-Sum(L2) as L4 from @test group by L1) b on a.L1=b.L1
/*
L1 L2 L3
-------------------------------------------------- ----------- -----------
A1 10 10
A1 10 5
A1 5 5
B1 10 20
B1 5 5
L1 L2 L3 L4
-------------------------------------------------- ----------- ----------- -----------
A1 10 10 -5
A1 10 5 -5
A1 5 5 -5
B1 10 20 10
B1 5 5 10
(5 行受影响)
*/
declare @test table(L1 varchar(50),L2 int,L3 int)
insert into @test
select 'A1',10,10 union all
select 'A1',10,5 union all
select 'A1',5,5 union all
select 'B1',10,20 union all
select 'B1',5,5
select * from @test
select a.*,b.L4 from @test a join
(select L1,sum(L3)-Sum(L2) as L4 from @test group by L1) b on a.L1=b.L1
/*
L1 L2 L3
-------------------------------------------------- ----------- -----------
A1 10 10
A1 10 5
A1 5 5
B1 10 20
B1 5 5
L1 L2 L3 L4
-------------------------------------------------- ----------- ----------- -----------
A1 10 10 -5
A1 10 5 -5
A1 5 5 -5
B1 10 20 10
B1 5 5 10
(5 行受影响)
*/
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· DeepSeek+PageAssist实现本地大模型联网
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· 从 14 秒到 1 秒:MySQL DDL 性能优化实战
· AI工具推荐:领先的开源 AI 代码助手——Continue