Friday, May 17, 2013

Sql Server : Convert rows into columns


Convert rows into columns


This scenario can be seen as an example for converting rows into columns.
There was list of customer and product in table CustProd.

name
Product
cust1
decoder
cust2
cable
cust1
cable
cust2
package
cust3
decoder
cust3
cable

The result required should be in such format that one could see the products by customer, i.e.; in one row it should contain customer name and all the products it’s having. Say if max three products are there then, it should look like below

 name
Product1
Product2
Product3
cust1
cable
decoder
NULL
cust2
cable
NULL
package
cust3
cable
decoder
NULL

For getting such result, we can use below query

Or else



Please provide your feedback for the post, if you find this post useful.

1 comment: