Link to home
Start Free TrialLog in
Avatar of countrymeister
countrymeister

asked on

COnvert rows to columns

I have a table OrderLine

OrderNumber
OrderDate
OrderItemCode
OrderQty

I have another table OrderItem
with a single column OrderItemCode

The table OrderItem has five rows with OrderItemCode's A, B, C, D, E. This is a constant number of rows.

The table OrderLine has eight rows

o1 , 10/14/2011,  A , 10
o1 , 10/14/2011,  B , 12
o2 , 10/14/2011,  A , 6
o2 , 10/15/2011,  C , 5
o3 , 10/14,2011,  D , 11
o3 , 10/16/2011,  E , 9
o3 , 10/16/2011,  D , 5
o3 , 10/17/2011,  B , 11

I want to convert this to a table, with these seven columns

OrderNumber, OrderDate, A, B, C, D, E

And the final output should be as follows, since I wnt to group by OrderNumber and OrderDate


o1 , 10/14/2011, 10 , 12 , , , ,

o2 , 10/14/2011, 6  ,    , , , ,
o2 , 10/15/2011,    ,    , 5 , , ,
o3 , 10/14/2011,   ,    , ,11 , ,

o3 , 10/16/2011,   ,    , ,5 ,9 ,

o3 , 10/17/2011,   , 11   , , , ,
ASKER CERTIFIED SOLUTION
Avatar of sachinpatil10d
sachinpatil10d
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
order by ordernumber added in the below query for exact output

select * from 
(select * from OrderLine )
p
pivot
(sum(orderQty) for
orderitemcode in ([A],[B],[C],[D],[E])
) as pvt
order by ordernumber

Open in new window