Interview Preparation mode beta
Funny Facebook Status Funny Facebook Status
Enter your email address

What is PIVOT feature in SQL Server, in MS SQL Server?

+1 vote
asked 1 year ago in SQL Server Interview Questions and Answers by R (19,530 points) edited 1 year ago by R

1 Answer

Nice?Vote!
 
Best answer
PIVOT in SQL Server 2005/8 is used for crosstab queries. ie t
CarSales
--------------
CarModel        Year       TotalUnitsSold
-------------------------------------------------------
Accent              2008      200   
i10                     2008     100
Accent              2009      150
i20                    2010       100
Accent              2010         50

And if we want reslult year wise
Year   Accent  i20  i10
2008  200       0     100
2009  150       0          0
2010  50         0      100

Then we need pivoting the table using pivot option in sql server
Select
year,Accent,i20,i10
from
(Select  year ,carmodel,totalunitssold
from CarSales cs
PIVOT
(
sum(TotalUnitsSold) for carmodel
in ([Accent],[i20],]i10]
) As pvt
[Not verified as no sql server was available  at the time writing answer]
Verify and let me know it works
answered 1 year ago by siva (10,720 points)

Related questions