数据内容:
卡号;类型;小计金额
001;A;1
001;B;2
001;C;3
001;A;4
001;C;2
002;A;1
002;B;2
002;C;3要求显示:
卡号;A类型;B类型;C类型....小计金额
001;2;1;2...12
002;1;1;1...6
卡号;类型;小计金额
001;A;1
001;B;2
001;C;3
001;A;4
001;C;2
002;A;1
002;B;2
002;C;3要求显示:
卡号;A类型;B类型;C类型....小计金额
001;2;1;2...12
002;1;1;1...6
sum(case when 类型='A' then 1 else 0 end) as A类个数,
sum(case when 类型='B' then 1 else 0 end) as B类个数,
sum(case when 类型='C' then 1 else 0 end) as C类个数,
sum(case when 类型='D' then 1 else 0 end) as D类个数,
sum(case when 类型='E' then 1 else 0 end) as E类个数,
sum(case when 类型='F' then 1 else 0 end) as F类个数,
sum(小计金额) as 小计金额
from tablename
group by 卡号
数据库 where 类型='A'
UNION
select 卡号,0,小计金额,0,0,0,0 from
数据库 where 类型='B'
UNION
select 卡号,0,0,小计金额,0,0,0 from
数据库 where 类型='C'
UNION
select 卡号,0,0,0,小计金额,0,0 from
数据库 where 类型='D'
UNION
select 卡号,0,0,0,0,小计金额,0 from
数据库 where 类型='E'
UNION
select 卡号,0,0,0,0,0,小计金额 from
数据库 where 类型='F'
set @sql=''
select @sql=@sql+'sum(case when 类型='''+类型+''' then 小计金额 else 0 ) as '+类型+'类型 ,' from table group by 卡号
set @sql=left(@sql,len(@sql)-1)
set @sql ='select 卡号,'+@sql+' from table group by 卡号'
exec (@sql)
select 卡号,sum(小计金额) as A类,0 as B类,0 as C类,0 as D类,0 as E类,0 as F类 from
数据库 where 类型='A'
group by 卡号
UNION
select 卡号,0,sum(小计金额),0,0,0,0 from
数据库 where 类型='B'
group by 卡号
UNION
select 卡号,0,0,sum(小计金额),0,0,0 from
数据库 where 类型='C'
group by 卡号
UNION
select 卡号,0,0,0,sum(小计金额),0,0 from
数据库 where 类型='D'
group by 卡号
UNION
select 卡号,0,0,0,0,sum(小计金额),0 from
数据库 where 类型='E'
group by 卡号
UNION
select 卡号,0,0,0,0,0,sum(小计金额) from
数据库 where 类型='F'
group by 卡号
declare @s varchar(8000)
set @s=''
select @s=@s+',['+类型+'类型]=sum(case 类型 when '''+类型+''' then 1 else 0 end'
from(select distinct 类型 from 表) a order by 类型
exec('select 卡号'+@s+',小计金额=sum(小计金额) from 表 group by 卡号')
declare @s varchar(8000)
set @s=''
select @s=@s+',['+类型+'类型]=sum(case 类型 when '''+类型+''' then 1 else 0 end)'
from(select distinct 类型 from 表) a order by 类型
exec('select 卡号'+@s+',小计金额=sum(小计金额) from 表 group by 卡号')
go
create table 表(卡号 varchar(3),类型 varchar(1),小计金额 int)
insert into 表
select '001','A',1
union all select '001','B',2
union all select '001','C',3
union all select '001','A',4
union all select '001','C',2
union all select '002','A',1
union all select '002','B',2
union all select '002','C',3
go--查询
declare @s varchar(8000)
set @s=''
select @s=@s+',['+类型+'类型]=sum(case 类型 when '''+类型+''' then 1 else 0 end)'
from(select distinct 类型 from 表) a order by 类型
exec('select 卡号'+@s+',小计金额=sum(小计金额) from 表 group by 卡号')
go--删除测试表
drop table 表/*--测试结果
卡号 A类型 B类型 C类型 小计金额
---- ----------- ----------- ----------- -----------
001 2 1 2 12
002 1 1 1 6
--*/
,[A类型]=sum(case 类型 when 'A' then 1 else 0 end)
,[B类型]=sum(case 类型 when 'B' then 1 else 0 end)
,[C类型]=sum(case 类型 when 'C' then 1 else 0 end)
,[D类型]=sum(case 类型 when 'D' then 1 else 0 end)
,[E类型]=sum(case 类型 when 'E' then 1 else 0 end)
,[F类型]=sum(case 类型 when 'F' then 1 else 0 end)
,小计金额=sum(小计金额)
from 表 group by 卡号
create table 表(卡号 varchar(3),类型 varchar(1),小计金额 int)
insert into 表
select '001','A',1
union all select '001','B',2
union all select '001','C',3
union all select '001','A',4
union all select '001','C',2
union all select '002','A',1
union all select '002','B',2
union all select '002','C',3
go--查询
select 卡号
,[A类型]=sum(case 类型 when 'A' then 1 else 0 end)
,[B类型]=sum(case 类型 when 'B' then 1 else 0 end)
,[C类型]=sum(case 类型 when 'C' then 1 else 0 end)
,[D类型]=sum(case 类型 when 'D' then 1 else 0 end)
,[E类型]=sum(case 类型 when 'E' then 1 else 0 end)
,[F类型]=sum(case 类型 when 'F' then 1 else 0 end)
,小计金额=sum(小计金额)
from 表 group by 卡号go--删除测试表
drop table 表/*--测试结果
卡号 A类型 B类型 C类型 D类型 E类型 F类型 小计金额
---- ----------- ----------- ----------- ----------- ----------- ----------- -----
001 2 1 2 0 0 0 12
002 1 1 1 0 0 0 6(所影响的行数为 2 行)--*/