Desired output with given table data

前端 未结 1 1928
轻奢々
轻奢々 2021-01-14 05:07

Hi I have a table test its structure is given below:

**Testing**

  PK    C1    c2  
 ---------------
 1      v11  v12
 2      v21  v23
 3      v31  v32


        
相关标签:
1条回答
  • 2021-01-14 05:49

    No, it can't be done with PIVOT, but it can be done with UNPIVOT:

    SELECT
      Pk,
      "Key",
      value
    FROM Testing
    UNPIVOT (
      value FOR "Key" IN (C1, C2)
    )
    

    And when UNPIVOT is unavailable, I often unpivot like this:

    SELECT
      t.Pk,
      x."Key",
      CASE x."Key"
        WHEN 'C1' THEN t.C1
        WHEN 'C2' THEN t.C2
      END AS value
    FROM Testing t
    CROSS JOIN (
      SELECT 'C1' AS "Key" FROM DUAL UNION ALL
      SELECT 'C2' FROM DUAL
    ) x
    
    0 讨论(0)
提交回复
热议问题