我撰寫了查詢以獲得 SQL Server 中的第一個結果。
我想把這張表轉成以下格式。我需要幫助在 SQL Server 中旋轉此表。

uj5u.com熱心網友回復:
如果你想在 sql server 中透視表,你可以這樣做
select [Division],[December],[January],[February]
from(
select * from SOF
) tbl
pivot(
Min(Emp) for Month in ([December],[January],[February])
) pvt
此查詢將根據提供的資料獲取您的結果。這是如何PIVOT作業的。
select
<non pivot column 1>,
<non pivot column 2>,
<pivoted column 1>,
<pivoted column 2>,
............
from (
--Result from the query
select * from yourTable
) tbl
pivot(
<Aggregation function> For [<column that contains the values that will become column headers>]
IN ( [first pivoted column], [second pivoted column],
... [last pivoted column])
)
uj5u.com熱心網友回復:
您可以選擇簡單的 PIVOT,如下所示:
DECLARE @table table(division varchar(30), emp int, month varchar(30))
INSERT INTO @table
values
('AAA',50,'December')
,('BBB',100,'December')
,('AAA',200,'January')
,('BBB',150,'January')
,('AAA',60,'February')
,('BBB',70,'February ')
SELECT * FROM @table
pivot (sum(emp) for month in ([December],[January],[February])) as pvt
| 分配 | 十二月 | 一月 | 二月 |
|---|---|---|---|
| AAA | 50 | 200 | 60 |
| BBB | 100 | 150 | 70 |
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/422526.html
標籤:
下一篇:UNIONALL與CTE
