简体   繁体   English

T-SQL:将行转换为列

[英]T-SQL: convert row into column

I would have the data like this我会有这样的数据

Id ID Name名称 Attributes属性 Type类型
1 1个 Product_1产品_1 red红色的 color颜色
2 2个 Product_1产品_1 small小的 size尺寸
3 3个 Product_2产品_2 red红色的 color颜色
4 4个 Product_2产品_2 large size尺寸

and I want it to be like this我希望它是这样的

Name名称 Color颜色 Size尺寸
Product_1产品_1 red红色的 small小的
Product_2产品_2 red红色的 large

I have tried pivot but cannot find the way我试过 pivot 但找不到路

select Name, [Color], [Size] from 
(
    select Name, [Type] , Attributes
    from @temp_table ) AS sourceTable
PIVOT
(
    MAX(Attributes)
    FOR  [Type] IN ([Color], [Size])
) as pivot_table

I just found out that there is nothing wrong in my code.我刚刚发现我的代码没有任何问题。

select fcId, [Color], [Size] from 
(
    select Id, [Type] , Attribute
    from @temp_table ) AS sourceTable
PIVOT
(
    MAX(Attribute)
    FOR  [Type] IN ([Color], [Size])
) as pivot_table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM