SQLTeam.com | Weblogs | Forums

How to convert rows to columns in SQL Server 2012


#1

I would like to show the following data from 1 table with a row as a column but without aggregation:
Invoice SalesOrder SalesOrderLine RowNo LineShipDate ShipQty
011401 009869 2 1 2015-04-13 00:00:00.000 36.000
011401 009869 3 1 2015-04-13 00:00:00.000 1116.000
015313 009869 3 2 2015-06-11 00:00:00.000 168.000
014231 009869 4 1 2015-05-26 00:00:00.000 36.000
012459 009869 5 1 2015-04-28 00:00:00.000 12.000

The value of the RowNo column can increase or decrease dynamically.

I would like the above to show as follows:
Invoice SalesOrder SalesOrderLine 1-Date 1-ShipQty 2-Date 2-ShipQty
011401 009869 2 2015-04-13 00:00:00.000 36.000
011401 009869 3 2015-04-13 00:00:00.000 1116.000 2015-06-11 00:00:00.000 168.000
014231 009869 4 2015-05-26 00:00:00.000 36.000
012459 009869 5 2015-04-28 00:00:00.000 12.000

Please help.

Thank you.


#2

I don't understand why your source data for invoice 15313 appears at the end of the desired output for the second line, invoice 11401