34,596
社区成员
发帖
与我相关
我的任务
分享
SELECT
CASE LEFT(编号,2) WHEN '11' THEN 'A小区' WHEN '12'THEN 'B小区' ELSE 'C小区' END as 区,
SUM金额) as 金额
FROM tb
GROUP BY
LEFT(编号,2)
select case substring('编号',1,2) when '11' then 'A小区' when '12' then 'B小区' else 'C小区' end
,sum('金额')
from tb
group by substring('编号',1,2)
select
case left(编号,2) when '11' then 'A小区' when '12' then 'B小区' else 'C小区' end as 区,
sum(金额) as 金额
from
tb
group by
left(编号,2)
select case left(编号,2) when '11' then 'A小区' when '12' then 'B小区' when '13' then 'C小区' else '' end as a
,sum(金额) as 金额
from tab
group by case left(编号,2) when '11' then 'A小区' when '12' then 'B小区' when '13' then 'C小区' else '' end
use Tempdb
go
--> -->
if not object_id(N'Tempdb..#A') is null
drop table #A
Go
Create table #A([编号] int,[日期] Datetime,[类型] nvarchar(3),[金额] int)
Insert #A
select 12047,'2011-10-9',N'收视费',26 union all
select 12246,'2011-10-9',N'收视费',26 union all
select 11161,'2011-10-9',N'收视费',26 union all
select 11166,'2011-10-9',N'收视费',156 union all
select 11238,'2011-10-9',N'收视费',26 union all
select 13244,'2011-10-9',N'收视费',26 union all
select 13028,'2011-10-9 8:08',N'收视费',156 union all
select 13028,'2011-10-9',N'收视费',156
Go
select case left(编号,2) when '11' then N'A小区' when '12' then N'B小区' else N'C小区' END AS N'區',sum(金额) as 金额 from #A group by left(编号,2)
/*
區 金额
A小区 208
B小区 52
C小区 338
*/
select case left(编号,2) when '11' then 'A小区' when '12' then 'B小区' else 'C小区' end,sum(金额) as 金额 from tableA group by left(编号,2)