What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 14018 |  Welcome, Guest!   Register  Login
 Home > Interview Questions > SQL Server Interview Questions > PIVOT in SQL SERVER 2008 with an example ...

PIVOT in SQL SERVER 2008 with an example?

Interview question and answer by: CGN007 | Posted on: 7/20/2012 | Category: SQL Server Interview questions | Views: 1877 | | Points: 40


Answer:

PIVOT is a nonstandard relational operator that are supported by Transact-SQL. You can use it to manipulate a table-valued expression into another table. PIVOT rotates a table-valued expression by turning the unique values from one column in the expression into multiple columns in the output, and performs aggregations where they are required on any remaining column values that are wanted in the final output

Example
Input Table

VendorID EmployeeName Orders
--------------------------------------
1 Rahul 5
1 Sourav 3
1 Sunil 9
2 Rahul 8
2 Sourav 4
2 Sunil1 6

SELECT VendorID,[Rahul],[Sourav],[Sunil] 


FROM PurchaseOrder

PIVOT

( sum (Orders)FOR EmployeeName IN ([Rahul],[Sourav],[Sunil] )

) AS p



Output


VendorID Rahul Sourav Sunil
-------------------------------------
1 5 3 9
2 8 4 6

Asked In: Many Interviews | Alert Moderator 
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

Even more ... | Submit Interview Questions and win prizes!

More Interview Questions from CGN007

Even more ... | Submit Interview Questions and win prizes!


About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/25/2013 7:07:09 AM