C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






PIVOT Operator in SQL Server 2005


Posted Date: 18 Aug 2008    Resource Type: Code Snippets    Category: SQL

Posted By: Kundan Kumar Sinha       Member Level: Gold
Rating:     Points: 1



SQL Server 2005 introduces support for the PIVOT and UNPIVOT operators. These operators allow data to be viewed in different dimensions and allow aggregations to be displayed along those dimensions.

Create a Management Studio Project.

  • Click Start | All Programs | Microsoft SQL Server 2005 | SQL Server Management Studio. If asked to connect, enter SERVER Name and authentication details. Then click Connect.
  • Click File | New | Project.
  • In the New Project dialog box, click SQL Server Scripts.
  • Change the Name of the project.
  • Clear the Create directory for Solution checkbox.
  • Click OK to open the project.

    Create a table and populate it with data.

  • In the Solution Explorer, right-click Queries and then click New Query.
  • When prompted for connection information, enter SERVER Name and authentication details. Then click Connect.
  • Under Queries, right-click the new script file and click Rename.
  • Rename the script.
  • In the query editor, type the following code to create and populate a table.


    CREATE TABLE SalesOrderTotalsMonthly
    (
    CustomerID int NOT NULL,
    OrderMonth int NOT NULL,
    SubTotal money NOT NULL
    )
    GO


    INSERT SalesOrderTotalsMonthly(1,1,5500)
    INSERT SalesOrderTotalsMonthly(1,2,1200)
    INSERT SalesOrderTotalsMonthly(1,3,1700)
    INSERT SalesOrderTotalsMonthly(1,4,2500)
    INSERT SalesOrderTotalsMonthly(2,1,1500)
    INSERT SalesOrderTotalsMonthly(2,2,5500)
    INSERT SalesOrderTotalsMonthly(2,3,5000)
    INSERT SalesOrderTotalsMonthly(2,4,6500)
    INSERT SalesOrderTotalsMonthly(2,5,500)
    INSERT SalesOrderTotalsMonthly(3,1,1100)
    INSERT SalesOrderTotalsMonthly(3,2,9000)
    INSERT SalesOrderTotalsMonthly(3,3,6500)
    INSERT SalesOrderTotalsMonthly(4,1,1200)
    INSERT SalesOrderTotalsMonthly(4,2,1500)
    INSERT SalesOrderTotalsMonthly(4,3,900)
    INSERT SalesOrderTotalsMonthly(4,4,7500)



    Create a query that uses the PIVOT operator.

  • In the query editor, type the following query that will pivot the table so that the sales for each month are output by customer.


    SELECT * FROM SalesOrderTotalsMonthly
    PIVOT (SUM(SubTotal) FOR OrderMonth IN
    ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])) AS a


  • Select the statements you just typed.
  • Press F5 to execute the query.

    You should see a row for each customer and columns for each month across the top of the results. The customer's total order amounts for a given month are displayed.




  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    PIVOT Operator in SQL Server 2005  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Count record from the multiple tables
    Previous Resource: Converting Multiple Columns into single Row or Column
    Return to Discussion Resource Index
    Post New Resource
    Category: SQL


    Post resources and earn money!
     
    Related Resources



    dotNet Slackers   BizTalk Adaptors    Web Design

    web conferencing

    Contact Us    Privacy Policy    Terms Of Use