Unpivot SQL thingie

后端 未结 1 1008
-上瘾入骨i
-上瘾入骨i 2021-01-14 22:24

I have some data like:

Chocolate  Strawberies  Oranges
2          3            1
4          2            4

How do i get is back as

相关标签:
1条回答
  • 2021-01-14 22:49
    declare @TT table (
        Chocolate int,
        Strawberies int,
        Oranges int
    )
    
    
    INSERT INTO @TT
    SELECT 2, 3, 1
    union all select
    4, 2, 4
    
    select * from @TT
    
    
    SELECT
        typename,
        numericvalue
    FROM (
        SELECT
            Chocolate,
            Strawberies,
            Oranges
        FROM @TT
    ) p
    UNPIVOT (
        numericvalue
        FOR typename IN (Chocolate, Strawberies, Oranges)
    ) as unpvt
    order by typename
    
    0 讨论(0)
提交回复
热议问题