PIVOT row and column conversion in SQL

PIVOT rotates a table-valued expression by transforming unique values ​​in one column of the expression into multiple columns in the output, performing aggregations if necessary on any remaining column values ​​required in the final output. UNPIVOT does the opposite of PIVOT, converting a column of a table-valued expression to a column value.


In layman's terms: PIVOT is row-to-column, UNPIVOT is row-to-row

 

1. PIVOT example

1. Create a table

Create a sales table, where the year field represents the year, the quarter field represents the quarter, and the amount field represents the sales. The quarter field uses Q1, Q2, Q3, and Q4 to represent the first, second, third, and fourth quarters, respectively.

 
CREATE TABLE SalesByQuarter
    ( year INT, -- year
        quarter CHAR(2), -- quarter
        amount MONEY -- total amount
    )
 

2. Fill in the table data

Use the following procedure to fill in the table data.

 
SET NOCOUNT ON
    DECLARE @index INT
    DECLARE @q INT
    SET @index = 0
    DECLARE @year INT
    while (@index < 30)
    BEGIN
        SET @year = 2005 + (@index % 4)
        SET @q = (CAST((RAND() * 500) AS INT) % 4) + 1
        INSERT INTO SalesByQuarter VALUES (@year, 'Q' + CAST(@q AS CHAR(1)), RAND() * 10000.00)
        SET @index = @index + 1
    END
 

3. What if we want to compare the sales in each quarter of the year ? There are two methods:

 

(1) Query using the traditional Select CASE statement

In previous versions of SQL Server, a series of CASE statements and aggregation queries were used to convert row-level data into column-level data. While this approach gives the developer a high degree of control over the data returned, writing these queries can be cumbersome.

 
    SELECT year as year
        , sum (case when quarter = 'Q1' then amount else 0 end) 一季度
        , sum (case when quarter = 'Q2' then amount else 0 end) 二季度
        , sum (case when quarter = 'Q3' then amount else 0 end) 三季度
        , sum (case when quarter = 'Q4' then amount else 0 end) 四季度
    FROM SalesByQuarter GROUP BY year ORDER BY year DESC
 

The result obtained is as follows:
 


 

(2), using PIVOT


With the new PIVOT operator in SQL Server 2005, the CASE and GROUP BY statements are no longer required. (Every PIVOT query involves some type of aggregation, so you can ignore the GROUP BY statement.) The PIVOT operator allows us to do the same thing with a CASE statement query, but you can do it with less code, and see look prettier.
 

 
SELECT year as year, Q1 as first quarter, Q2 as second quarter, Q3 as third quarter, Q4 as fourth quarter FROM SalesByQuarter PIVOT (SUM (amount) FOR quarter IN (Q1, Q2, Q3, Q4) ) AS P ORDER BY YEAR DESC
 

The result obtained is as follows:
 

 

 

二、通过下面一个实例详细介绍PIVOT的过程

 

 
SELECT [星期一],[星期二],[星期三],[星期四],[星期五],[星期六],[星期日]--这里是PIVOT第三步(选择行转列后的结果集的列)这里可以用“*”表示选择所有列,也可以只选择某些列(也就是某些天)
FROM WEEK_INCOME --这里是PIVOT第二步骤(准备原始的查询结果,因为PIVOT是对一个原始的查询结果集进行转换操作,所以先查询一个结果集出来)这里可以是一个select子查询,但为子查询时候要指定别名,否则语法错误
PIVOT
(
    SUM(INCOME) for [week] in([星期一],[星期二],[星期三],[星期四],[星期五],[星期六],[星期日])--这里是PIVOT第一步骤,也是核心的地方,进行行转列操作。聚合函数SUM表示你需要怎样处理转换后的列的值,是总和(sum),还是平均(avg)还是min,max等等。例如如果week_income表中有两条数据并且其week都是“星期一”,其中一条的income是1000,另一条income是500,那么在这里使用sum,行转列后“星期一”这个列的值当然是1500了。后面的for [week] in([星期一],[星期二])中 for [week]就是说将week列的值分别转换成一个个列,也就是“以值变列”。但是需要转换成列的值有可能有很多,我们只想取其中几个值转换成列,那么怎样取呢?就是在in里面了,比如我此刻只想看工作日的收入,在in里面就只写“星期一”至“星期五”(注意,in里面是原来week列的值,"以值变列")。总的来说,SUM(INCOME) for [week] in([星期一],[星期二],[星期三],[星期四],[星期五],[星期六],[星期日])这句的意思如果直译出来,就是说:将列[week]值为"星期一","星期二","星期三","星期四","星期五","星期六","星期日"分别转换成列,这些列的值取income的总和。
)TBL--别名一定要写
 
三.UNPIVOT
 
 
很明显,UN这个前缀表明了,它做的操作是跟PIVOT相反的,即列转行。UNPIVOT操作涉及到以下三个逻辑处理阶段。
 
1,生成副本
2,提取元素
3,删除带有NULL的行
 
 
UNPIVOT实例
 
CREATE TABLE pvt (VendorID int, Emp1 int, Emp2 int,
    Emp3 int, Emp4 int, Emp5 int);
GO
INSERT INTO pvt VALUES (1,4,3,5,4,4);
INSERT INTO pvt VALUES (2,4,1,5,5,5);
INSERT INTO pvt VALUES (3,4,3,5,4,4);
INSERT INTO pvt VALUES (4,4,2,5,5,4);
INSERT INTO pvt VALUES (5,5,1,5,5,5);
GO
--Unpivot the table.
SELECT VendorID, Employee, Orders
FROM 
   (SELECT VendorID, Emp1, Emp2, Emp3, Emp4, Emp5
   FROM pvt) p
UNPIVOT
   (Orders FOR Employee IN 
      (Emp1, Emp2, Emp3, Emp4, Emp5)
)AS unpvt;
GO
 

上面UNPIVOT实例的分析

 

UNPIVOT的输入是左表表达式P,第一步,先为P中的行生成多个副本,在UNPIVOT中出现的每一列,都会生成一个副本。因为这里的IN子句有5个列名称,所以要为每个来源行生成5个副本。结果得到的虚拟表中将新增一个列,用来以字符串格式保存来源列的名称(for和IN之间的,上面例子是 Employee )。第二步,根据新增的那一列中的值从来源列中提取出与列名对应的行。第三步,删除掉结果列值为null的行,完成这个查询。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326167381&siteId=291194637