Home Forums SQL Server 2008 T-SQL (SS2K8) Need help in converting rows into column... include attached image file in detail RE: Need help in converting rows into column... include attached image file in detail

  • I think that you may have oversimplified your real data....??

    do you know in advance the maximum number of items that are allowed?

    anyways here is some code that does what you asked for, based on the limited sample data provided.

    it uses the concept in these two fine articles (as Lutz suggested)

    http://www.sqlservercentral.com/articles/T-SQL/63681/

    http://www.sqlservercentral.com/articles/T-SQL/63681/

    assuming you did read these...then maybe you are struggling in having to do two separate pivots and rejoin them to meet your specific layout requirements.......

    ;with cte as

    (

    SELECT SalesOrder,

    max(case when itemno =1 then itemname else '' end) as item1,

    max(case when itemno =2 then itemname else '' end) as item2,

    max(case when itemno =3 then itemname else '' end) as item3,

    rn =1

    FROM Table1

    GROUP BY SalesOrder

    UNION ALL

    SELECT SalesOrder,

    max(case when itemno =1 then cast(price as varchar) else '' end) as item1,

    max(case when itemno =2 then cast(price as varchar) else '' end) as item2,

    max(case when itemno =3 then cast(price as varchar) else '' end) as item3,

    rn =2

    FROM Table1

    GROUP BY SalesOrder

    )

    SELECT salesorder,

    item1,

    item2,

    item3

    FROM cte

    ORDER BY salesorder, rn

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day