2012-06-15 3 views
0

에서이 나는피벗 SQL

SalesOrderId Partners PartnerType 
1     A   1 
1     B   2 
2     X   1 
2     Y   2 

내가 선회하여이를 달성하는 것이 가능

SalesOrdeId Reseller Distrubutor ResellerType DistributorType 
1    A    B   1    2 
2    X    Y   1    2 

N.B. ~ 리셀러 유형 = 1, 대리점 유형 = 2

필요 있나요?

DDL

declare @t table(SalesOrderId int,Partners Varchar(10),PartnerType int) 
Insert into @t values(1,'A',1),(1,'B',2),(2,'X',1),(2,'Y',2) 
select * 
From @t 

감사

답변

1
select SalesOrderId, 
     max(case PartnerType when 1 then Partners end) as Reseller, 
     max(case PartnerType when 2 then Partners end) as Distributor, 
     1 as ResellerType, 
     2 as DistributorType 
from @t 
group by SalesOrderId