How to make awkward pivot of sql table in SQL Server 2005?

后端 未结 3 1186
忘掉有多难
忘掉有多难 2021-01-05 08:58

I have to rotate a given table from an SQL Server but a normal pivot just doesn\'t work (as far as i tried). So has anybody an idea how to rotate the table into the desired

3条回答
  •  走了就别回头了
    2021-01-05 09:49

    select
    id,
    'Numerator' as ValueType,
    case when label = labelNameOne then Numerator else 0 end as LabelNameOne,
    case when label = labelNameTwo then Numerator else 0 end as LabelNameTwo,
    case when label = labelNameTree then Numerator else 0 end as LabelNameTree,
    case when label = labelNameFour then Numerator else 0 end as LabelNameFour,
    case when label = labelNameFive then Numerator else 0 end as LabelNameFive,
    case when label = labelNameSix then Numerator else 0 end as LabelNameSix
    
    union All
    

    ... similar query with Denominator ...

    union all
    

    ... similar query with Ratio...

提交回复
热议问题