COnvert rows to columns

Posted on 2011-10-18
Last Modified: 2012-08-13
I have a table OrderLine


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   , , , ,
Question by:countrymeister
    LVL 9

    Accepted Solution

    Try this
    select * from 
    (select * from OrderLine )
    (sum(orderQty) for
    orderitemcode in ([A],[B],[C],[D],[E])
    ) as pvt

    Open in new window

    LVL 9

    Expert Comment

    order by ordernumber added in the below query for exact output

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

    Open in new window


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
    Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now