Database Pivoting - what is the purpose?

前端 未结 3 1766
醉话见心
醉话见心 2021-01-02 03:41

Why would someone want to convert columns into rows (ie pivoting)? Which problem does it solve?

I found some links, but did not get satisfactory answers to the quest

3条回答
  •  迷失自我
    2021-01-02 03:56

    How about this as an example?

    How to PIVOT Data Using T-SQL

    A common expectation in data extraction is the ability to transform the output of multiple rows into multiple columns in a single row. SQL Server 2005/2008 provide the ability to do this with the PIVOT operator in a Query.

    EDIT

    Lets say you have a table that stores sales per customer by date

    Something like

    Table
    - SaleDate
    - CustomerID
    - SaleAmount
    

    Using PIVOT you can display a grid that totals sales per client by month/quarter/year

        |Client A|Client B|Client C
    --------------------------------
    2007| 100    | 0      | 150
    2008| 0      | 200    | 160
    2009| 110    | 180    | 100
    

    This would purely be for summary purposes.

提交回复
热议问题