34,588
社区成员
发帖
与我相关
我的任务
分享
create table #a (CC varchar(10),MODEL varchar(20),STATUS char(1))
go
insert into #a values( 'GSM','SGH-T108+','Z')
insert into #a values('CDMA','DVD-P248A','H')
insert into #a values('CDMA','STW-CG72', 'C')
insert into #a values('CDMA','STW-CG72', 'H')
insert into #a values('CDMA','N70', 'A')
insert into #a values( 'GSM','SGH-T108+','Z')
insert into #a values( 'GSM','SGH-T108+','Z')
insert into #a values( 'GSM','SGH-T108+','G')
select * from #a
declare @sql varchar(8000)
select @sql = 'select CC,MODEL'
select @sql = @sql + ',sum(case when STATUS = ''' +STATUS+ ''' then 1 else 0 end) as '''+STATUS+''''
from (select distinct STATUS from #a) a
select @sql = @sql + ' from #a group by CC,MODEL'
exec (@sql)
drop table #a
create table tb(gc varchar(10),MODEL varchar(50),status varchar(10))
insert into tb select 'GSM','SGH-T108+','z'
insert into tb select 'CDMA','DVD-P248A','h'
insert into tb select 'CDMA','STW-CG72','c'
insert into tb select 'CDMA','STW-CG72','h'
insert into tb select 'CDMA','N70','a'
insert into tb select 'GSM','SGH-T108+','z'
insert into tb select 'GSM','SGH-T108+','z'
insert into tb select 'GSM','SGH-T108+','g'
select top 25 id=identity(int,0,1),status='a' into # from sysobjects a,sysobjects b
update # set status=char(ascii(status) +id)
declare @sql varchar(8000)
select @sql = isnull(@sql+',','') + 'sum(case when status=''' + status + ''' then 1 else 0 end) [' + status + ']'
from #
set @sql ='select gc,MODEL,'+ @sql + ' from tb group by gc,MODEL'
exec(@sql)
gc MODEL a b c d e f g h i j k l m n o p q r s t u v w x y
CDMA DVD-P248A 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CDMA N70 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
GSM SGH-T108+ 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CDMA STW-CG72 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
create table tb(gc varchar(10),MODEL varchar(50),status varchar(10))
insert into tb select 'GSM','SGH-T108+','z'
insert into tb select 'CDMA','DVD-P248A','h'
insert into tb select 'CDMA','STW-CG72','c'
insert into tb select 'CDMA','STW-CG72','h'
insert into tb select 'CDMA','N70','a'
insert into tb select 'GSM','SGH-T108+','z'
insert into tb select 'GSM','SGH-T108+','z'
insert into tb select 'GSM','SGH-T108+','g'
declare @sql varchar(8000)
select @sql = isnull(@sql+',','') + 'sum(case when status=''' + status + ''' then 1 else 0 end) [' + status + ']'
from (select 'a' as status union all
select 'b' as status union all
select 'c' as status union all
select 'd' as status union all
select 'e' as status union all
select 'f' as status union all
select 'g' as status union all
select 'h' as status union all
select 'i' as status union all
select 'z' as status union all
select 'k' as status
) as a
set @sql ='select gc,MODEL,'+ @sql + ' from tb group by gc,MODEL'
exec(@sql)
--这是2000中用函数的方法,2005的方法见上.
create table tb(GC varchar(10), MODEL varchar(20), STATUS varchar(10))
insert into tb values('GSM' , 'SGH-T108+', 'Z')
insert into tb values('CDMA', 'DVD-P248A', 'H')
insert into tb values('CDMA', 'STW-CG72' , 'C')
insert into tb values('CDMA', 'STW-CG72' , 'H')
insert into tb values('CDMA', 'N70' , 'A')
insert into tb values('GSM' , 'SGH-T108+', 'Z')
insert into tb values('GSM' , 'SGH-T108+', 'Z')
insert into tb values('GSM' , 'SGH-T108+', 'G')
go
--创建一个合并的函数
create function f_hb(@GC varchar(10) , @MODEL varchar(20))
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ' ' + cast(STATUS as varchar) from tb where GC = @GC and MODEL = @MODEL
set @str = right(@str , len(@str) - 1)
return(@str)
End
go
--调用自定义函数得到结果:
select distinct GC , MODEL , dbo.f_hb(GC , MODEL) as STATUS from tb
drop table tb
drop function f_hb
/*
GC MODEL STATUS
---------- -------------------- -------
CDMA DVD-P248A H
CDMA N70 A
CDMA STW-CG72 C H
GSM SGH-T108+ Z Z Z G
(所影响的行数为 4 行)
*/