sql server如何求前N列的和(具体几列未知) 右侧汇总
首先创建数据,以小区收费为例:
select identity(int,1,1) as ID,tb.* into #Test from(
Select 100 as 电费,20 as 水费, 25 as 物业费,180 as 租金
union all
Select 90 as 电费,120 as 水费,125 as 物业费,10 as 租金
union all
Select 10 as 电费,210 as 水费,225 as 物业费,100 as 租金) tb
ID 电费 水费 物业费 租金
----------- ----------- ----------- ----------- -----------
1 100 20 25 180
2 90 120 125 10
3 10 210 225 100
① 若已知具体费用名称可以通过下面的方法实现:
Select *,电费+水费+物业费+租金 as 总费用 from #Test
ID 电费 水费 物业费 租金 总费用
----------- ----------- ----------- -----------