Pivoting in SQL

I have

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

I need

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

N.B.~ Reseller Type = 1, Distributor Type = 2

Is it possible to achieve this using pivoting?

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

Thanks

Answers


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

Need Your Help

A Question about using jython when run a receving socket in python

python sockets jython nonblocking

I have not a lot of knowledge of python and network programming. Currently I am trying to implement a simple application which can receive a text message sent by the user, fetch some information fr...

Writing a script that can be both piped to and given direct input

windows shell scripting cmd pipe

This is a follow-on question from here, posted at the suggestion of the person who originally answered it.