0
Original
RecordKey Name Section1_Product Section1_Code Section2_Product Section2_Code ......
1 a ff 22
2 b gg 22
3 c hh 33
RecordKey Name Section Product Code ......
1 a 1 ff 22
1 a 2
2 b 1 gg 22
2 b 2
3 c 1 hh 22
3 c 2
我想将列转化为行。有些部分会有空值。Unpivot多列不显示欲望结果
SELECT RecordKey
,Name
,'Num_of_Sections' = ROW_NUMBER() OVER (PARTITION BY RecordKey ORDER BY ID)
,Product
,Code
FROM (
SELECT RecordKey, Name, Section1_Product, Section1_Code, Section2_Product, Section2_Code FROM Table
) M
UNPITVOT (
Product FOR ID IN (Section1_Product, Section2_Product)
) p
UNPIVOT (
Code FOR CO IN (Section1_Code, Section2_Code)
) c
如果我只执行一列(Product,注释掉代码),那么我将在ID列(1,2)中有2个值。如果我用2列运行查询,那么我在ID列(1,2,3,4)中得到4个值。
谢谢你帮助我的假设和数据。 Section2_Product&Section2_Code可以具有值或可以为空,并且该文件具有多于两个部分。 – user1804925
只需在NULL Place中添加值并在Cross Apply中添加该部分和代码就像1&2 @ user1804925 – mohan111