查询以获取股票分类的自定义数据视图

时间:2018-10-09 11:09:55

标签: sql oracle

我有如下所示的数据,其中我有股票以及它们的1类和2类 如下所示,需要帮助以实现所需的查询结果。我尝试了几种基本方法,但无法继续前进。任何帮助将不胜感激。 因此,想法是将class1放在顶部,并为第3列中的所有行迭代所有class2。

表格日期:

enter image description here

预期数据输出:

enter image description here

1 个答案:

答案 0 :(得分:0)

您可能想要以下内容,但是我不确定这是否正是您要查找的内容,因为我不知道字符串“ S,3,TESTxxx,US”中的S和US在哪里来自:

WITH dat AS (SELECT 'INFOTECH' Class1, 'SECOSEEQ' Class2, 'TEST123' Asset FROM dual
              UNION ALL
             SELECT 'INFOTECH' Class1, 'SECOSEEQ' Class2, 'TEST124' Asset FROM dual
              UNION ALL
             SELECT 'INFOTECH' Class1, 'SECOSEEQ' Class2, 'TEST125' Asset FROM dual
              UNION ALL
             SELECT 'INFOTECH' Class1, 'SOSE' Class2, 'TEST126' Asset FROM dual
              UNION ALL
             SELECT 'INFOTECH' Class1, 'SOSE' Class2, 'TEST127' Asset FROM dual
              UNION ALL
             SELECT 'INFOTECH' Class1, 'SOSE' Class2, 'TEST128' Asset FROM dual
              UNION ALL
             SELECT 'INFOTECH' Class1, 'TEHAEQ' Class2, 'TEST129' Asset FROM dual
              UNION ALL
             SELECT 'INFOTECH' Class1, 'TEHAEQ' Class2, 'TEST130' Asset FROM dual
              UNION ALL
             SELECT 'MATERIAL' Class1, 'ALUMINUM' Class2, 'TEST131' Asset FROM dual
              UNION ALL
             SELECT 'MATERIAL' Class1, 'ALUMINUM' Class2, 'TEST132' Asset FROM dual
              UNION ALL
             SELECT 'MATERIAL' Class1, 'CHEMICAL' Class2, 'TEST133' Asset FROM dual
              UNION ALL
             SELECT 'MATERIAL' Class1, 'CHEMICAL' Class2, 'TEST134' Asset FROM dual)
SELECT CASE WHEN class2 IS NULL 
            THEN '1,'||class1
            WHEN asset IS NULL 
            THEN '2,'||class2
            ELSE 'S,3,'||asset||',US'
       END  
  FROM dat
 GROUP BY class1, ROLLUP(class2,asset)
 ORDER BY class1, class2 nulls first, asset nulls first