本文介紹了使用 UNPIVOT 將行轉換為列的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
以下是我的示例數據集:
Following is my sample dataset:
ID Prod1 Prod2 Prod3
1 ABC01 CDE02 XYZ03
我想將行轉換為列,我想要的輸出是:
I want to convert rows to columns and my desired output is:
ID Products
1 ABC01
1 CDE02
1 XYZ03
我嘗試使用 UNPIVOT,但我的代碼不起作用:
I tried using UNPIVOT, but my code didn't work:
Select Id, Products
From Sample
UNPIVOT
(
Products
FOR Id IN ([1],[2],[3])
) AS P
有人可以幫我將這些行轉換為每個 ID 的列嗎?
Can someone please help me converting these rows to columns for each ID?
推薦答案
或者,您可以使用 VALUES
子句來取消數據透視:
Alternatively, you can use a VALUES
clause to unpivot the data:
SELECT S.ID,
V.Product
FROM dbo.Sample S
CROSS APPLY (VALUES(S.Prod1),(S.Prod2),(S.Prod3))V(Product);
DB<>小提琴
這篇關于使用 UNPIVOT 將行轉換為列的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!