In specific case we have to convert rows into column in sql
server. Here we use pivot function to convert rows into column.
Create table ProductDetails(Cust
varchar(50),Product varchar(30),Qty int)
Insert into ProductDetails(Cust,Product,Qty)
Values('MacD','Burger',10)
Insert into ProductDetails(Cust,Product,Qty)
Values('MacD','Coke',12)
Insert into ProductDetails(Cust,Product,Qty)
Values('KFC','Chicken',10)
Insert into ProductDetails(Cust,Product,Qty)
Values('KFC','Burger',10)
Insert into ProductDetails(Cust,Product,Qty)
Values('Dominos','Burger',10)
Insert into ProductDetails(Cust,Product,Qty)
Values('Dominos','Pizza',10)
Select Product,KFC,MacD,Dominos
from
(Select Product,Cust,Qty from ProductDetails)
P
pivot(Sum(Qty) for Cust in(KFC,MacD,Dominos)) as pvt
order by product
OutPut:-
No comments:
Post a Comment
Note: only a member of this blog may post a comment.