SQL2005/2008中實現行轉列的2種方法
阿新 • • 發佈:2019-01-10
CREATE TABLE sales.salesByMonth(year char(4),month char(3),amount money,PRIMARY KEY (year, month))INSERT INTO sales.salesByMonth (year, month, amount)VALUES('2004','Jan', 789.0000)INSERT INTO sales.salesByMonth (year, month, amount)VALUES('2004','Feb', 389.0000)INSERT INTO sales.salesByMonth (year, month, amount)VALUES('2004','Mar', 8867.0000)INSERT INTO sales.salesByMonth (year, month, amount)VALUES('2004','Apr', 778.0000)INSERT INTO sales.salesByMonth (year, month, amount)VALUES('2004','May', 78.0000)INSERT INTO sales.salesByMonth (year, month, amount)VALUES('2004','Jun', 9.0000)INSERT INTO sales.salesByMonth (year, month, amount)VALUES('2004','Jul', 987.0000)INSERT INTO sales.salesByMonth (year, month, amount)VALUES('2004','Aug', 866.0000)INSERT INTO sales.salesByMonth (year, month, amount)VALUES('2004','Sep', 7787.0000)INSERT INTO sales.salesByMonth (year, month, amount)VALUES('2004','Oct', 85576.0000)INSERT INTO sales.salesByMonth (year, month, amount)VALUES('2004','Nov', 855.0000)INSERT INTO sales.salesByMonth (year, month, amount)VALUES('2004','Dec', 5878.0000)INSERT INTO sales.salesByMonth (year, month, amount)VALUES('2005','Jan', 7.0000)INSERT INTO sales.salesByMonth (year, month, amount)VALUES('2005','Feb', 6868.0000)INSERT INTO sales.salesByMonth (year, month, amount)VALUES('2005','Mar', 688.0000)INSERT INTO sales.salesByMonth (year, month, amount)VALUES('2005','Apr', 9897.0000)
二、對上述資料進行行轉列
方法一:大家都常用的方法:select case
SELECT year,
SUM(case when month = 'Jan' then amount else 0 end) AS 'Jan',
SUM(case when month = 'Feb' then amount else 0 end) AS 'Feb',
SUM(case when month = 'Mar' then amount else 0 end) AS 'Mar',
SUM(case when month = 'Apr' then amount else 0 end) AS 'Apr',
SUM(case when month = 'May' then amount else 0 end) AS 'May',
SUM(case when month = 'Jun' then amount else 0 end) AS 'Jun',
SUM(case when month = 'Jul' then amount else 0 end) AS 'Jul',
SUM(case when month = 'Aug' then amount else 0 end) AS 'Aug',
SUM(case when month = 'Sep' then amount else 0 end) AS 'Sep',
SUM(case when month = 'Oct' then amount else 0 end) AS 'Oct',
SUM(case when month = 'Nov' then amount else 0 end) AS 'Nov',
SUM(case when month = 'Dec' then amount else 0 end) AS 'Dec'
FROM sales.salesByMonth
GROUP by year 方法二:SQL2005/2008中的 Pivot 方法 SELECT Year,[Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec]
FROM
(
SELECT year, amount, month
FROM sales.salesByMonth
) AS salesByMonth
PIVOT
(
SUM(amount) FOR month
IN
([Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec])
) AS MyPivot
ORDER BY Year
對上述Pivot語法結構的詳細解釋如下:
Povit:旋轉
Pivoted:旋轉的
Query:查詢
Produce:生成、產生
alias:別名、化名
aggregation:聚合
optional:可選擇的
clause:子句
PIVOT 提供的語法比一系列複雜的 SELECT...CASE 語句中所指定的語法更簡單和更具可讀性。有關
PIVOT 語法的完整說明,請參閱 FROM (Transact-SQL)。
以下是帶批註的 PIVOT 語法。
SUM(case when month = 'Jan' then amount else 0 end) AS 'Jan',
SUM(case when month = 'Feb' then amount else 0 end) AS 'Feb',
SUM(case when month = 'Mar' then amount else 0 end) AS 'Mar',
SUM(case when month = 'Apr' then amount else 0 end) AS 'Apr',
SUM(case when month = 'May' then amount else 0 end) AS 'May',
SUM(case when month = 'Jun' then amount else 0 end) AS 'Jun',
SUM(case when month = 'Jul' then amount else 0 end) AS 'Jul',
SUM(case when month = 'Aug' then amount else 0 end) AS 'Aug',
SUM(case when month = 'Sep' then amount else 0 end) AS 'Sep',
SUM(case when month = 'Oct' then amount else 0 end) AS 'Oct',
SUM(case when month = 'Nov' then amount else 0 end) AS 'Nov',
SUM(case when month = 'Dec' then amount else 0 end) AS 'Dec'
FROM sales.salesByMonth
GROUP by year 方法二:SQL2005/2008中的 Pivot 方法 SELECT
SELECT <non-pivoted column>,
[first pivoted column] AS <column name>,
[second pivoted column] AS <column name>,
...
[last pivoted column] AS <column name>
FROM
(<SELECT query that produces the data>)
AS <alias for the source query>
PIVOT
(
<aggregation function>(<column being aggregated>)
FOR
[<column that contains the values that will become column headers>]
IN ( [first pivoted column], [second pivoted column],
... [last pivoted column])
) AS <alias for the
pivot table>
<optional ORDER BY clause>;