【问题标题】:Splitting row value into specific columns based on the value根据值将行值拆分为特定列
【发布时间】:2023-03-14 09:13:01
【问题描述】:

我有一张这样的表,它实际上是在管理 VSTS 中的障碍

Team Name   Item ID    Score   Score Label
-------------------------------------------
Team1         1          2     Green
Team1         2          0     Red
Team1         3          1     Amber
Team1         4          2     Green
Team1         5          0     Red
Team2         6          1     Amber
Team2         7          0     Red
Team3         8          2     Green

但我需要以不同的方式呈现这个

Team Name   Item ID    Score   Green    Amber     Red
------------------------------------------------------
Team1         1          2       1       0         0
Team1         2          0       0       0         1
Team1         3          1       0       1         0
Team1         4          2       1       0         0
Team1         5          0       0       0         1
Team2         6          1       0       1         0
Team2         7          0       0       0         1
Team3         8          2       1       0         0

我相信 SQL 透视是最好的方法。但我不是支点专家。有人可以帮我解决这个问题吗?

【问题讨论】:

标签: sql sql-server-2012 pivot


【解决方案1】:

case 表达式可能是最快的解决方案:

    SELECT Team Name, Item ID, Score
    , case when Score Label = 'Green' then 1 else 0 end as 'Green'
    , case when Score Label = 'Amber' then 1 else 0 end as 'Amber'
    , case when Score Label = 'Red' then 1 else 0 end as 'Red'
    from Table

【讨论】:

    猜你喜欢
    • 2022-01-24
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多