日期:2014-05-18  浏览次数:20574 次

这种条件用sqlserver怎么写
AA BB CC --字段名
1 a A 
1 a B  
1 b A 
1 b C 
2 a C
2 a B
变成
AA A B C
1 a a
1 b - b
2 - a a


------解决方案--------------------
探讨

SQL code

declare @t table (AA int,BB varchar(1),CC varchar(1))
insert into @t
select 1,'a','A' union all
select 1,'a','B' union all
select 1,'b','A' union all
select 1,'b','C' union all
select 2,'a……

------解决方案--------------------
SQL code
declare @t table (AA int,BB varchar(1),CC varchar(1))
insert into @t
select 1,'a','A' union all
select 1,'a','B' union all
select 1,'b','A' union all
select 1,'b','C' union all
select 2,'a','C' union all
select 2,'a','B'
SELECT * FROM @t

SELECT AA,a=ISNULL(a,'-'),b=ISNULL(b,'-'),c=ISNULL(c,'-') FROM
(SELECT *,dd=bb FROM @t) a
PIVOT
(MAX(dd) FOR CC IN ([A],[B],[C])) b
ORDER BY AA
/*
AA    a    b    c
1    a    a    -
1    b    -    b
2    -    a    a
*/