2017-06-26 71 views
0

考慮這個例子: Pivoted Table如何在沒有UNPIVOT運算符的情況下UNPIVOT表格?

和所需的輸出如下: Desired Result

所以我的問題是。我正在運行一個專有的服務器,我不能在這裏提及這個服務器不支持UNPIVOT運營商。到目前爲止,下面我曾嘗試:

SELECT week, 
     day, 
     username, 
     CASE 
      WHEN main_cat_1 = "cars" THEN "cars" 
      WHEN main_cat_2 = "vans" THEN "vans" 
     ELSE null END AS main_cat, 
     CASE 
      WHEN sub_cat_1.1 = "dirty" THEN "dirty" 
      WHEN sub_cat_1.2 = "stalled" THEN "stalled" 
     ELSE null END AS sub_cat 
     FROM table1 

這種方法的問題是,由於短路評價第一個真正的valueit後似乎並沒有正常工作,並且不會返回預期值。

+0

您的查詢是無效的標準SQL。你正在使用哪個DBMS? –

+0

請閱讀http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question/285557和接受的答案 –

回答

1
SELECT week, 
    day, 
    username, 
    main_cat_1 as category, 
    sub_cat_1.1 as subcategory 
FROM table1 
UNION 
SELECT week, 
    day, 
    username, 
    main_cat_1, 
    sub_cat_1.2 
FROM table1 
UNION 
...etc.