将未知行数合并为一行

时间:2016-02-18 08:00:16

标签: sql oracle

所以我想说我有以下sql结果:

+----------+---------------+--------------+--------------+
| "FALLID" | "PGCH_STATUS" | "PKA_STATUS" | "ZOU_STATUS" |
+----------+---------------+--------------+--------------+
| "687099" | "Grey"        | "Grey"       | "Yellow"     |
| "687099" | "Yellow"      | "Grey"       | "Grey"       |
+----------+---------------+--------------+--------------+

来自这个声明:

CREATE OR REPLACE VIEW UDV_GKH_ABT_STATUS AS
SELECT f.fallid,
CASE
        WHEN CB_FA LIKE 'ECH' AND C_FA_FREIGABE LIKE 'J' AND C_ABT_ABSCHLUSS LIKE 'N' THEN 'Yellow'
        WHEN CB_FA LIKE 'ECH' AND C_FA_FREIGABE LIKE 'J' AND C_ABT_ABSCHLUSS LIKE 'J' THEN 'Green'
        ELSE 'Grey'
    END AS ECH_Status,
    CASE
        WHEN CB_FA LIKE 'EGE' AND C_FA_FREIGABE LIKE 'J' AND C_ABT_ABSCHLUSS LIKE 'N' THEN 'Yellow'
        WHEN CB_FA LIKE 'EGE' AND C_FA_FREIGABE LIKE 'J' AND C_ABT_ABSCHLUSS LIKE 'J' THEN 'Green'
        ELSE 'Grey'
    END AS EGE_Status,
    CASE
        WHEN (CB_FA LIKE 'EGY' OR CB_FA LIKE 'EGW') AND C_FA_FREIGABE LIKE 'J' AND C_ABT_ABSCHLUSS LIKE 'N' THEN 'Yellow'
        WHEN (CB_FA LIKE 'EGY' OR CB_FA LIKE 'EGW') AND C_FA_FREIGABE LIKE 'J' AND C_ABT_ABSCHLUSS LIKE 'J' THEN 'Green'
        ELSE 'Grey'
    END AS EGY_Status,
    CASE
        WHEN CB_FA LIKE 'EIM' AND C_FA_FREIGABE LIKE 'J' AND C_ABT_ABSCHLUSS LIKE 'N' THEN 'Yellow'
        WHEN CB_FA LIKE 'EIM' AND C_FA_FREIGABE LIKE 'J' AND C_ABT_ABSCHLUSS LIKE 'J' THEN 'Green'
        ELSE 'Grey'
    END AS EIM_Status,
    CASE
        WHEN (CB_FA LIKE 'EAN' OR CB_FA LIKE 'PAN') AND C_FA_FREIGABE LIKE 'J' AND C_ABT_ABSCHLUSS LIKE 'N' THEN 'Yellow'
        WHEN (CB_FA LIKE 'EAN' OR CB_FA LIKE 'PAN') AND C_FA_FREIGABE LIKE 'J' AND C_ABT_ABSCHLUSS LIKE 'J' THEN 'Green'
        ELSE 'Grey'
    END AS GAN_Status,
    CASE
        WHEN CB_FA LIKE 'PGCH' AND C_FA_FREIGABE LIKE 'J' AND C_ABT_ABSCHLUSS LIKE 'N' THEN 'Yellow'
        WHEN CB_FA LIKE 'PGCH' AND C_FA_FREIGABE LIKE 'J' AND C_ABT_ABSCHLUSS LIKE 'J' THEN 'Green'
        ELSE 'Grey'
    END AS PGCH_Status,
    CASE
        WHEN CB_FA LIKE 'PKA' AND C_FA_FREIGABE LIKE 'J' AND C_ABT_ABSCHLUSS LIKE 'N' THEN 'Yellow'
        WHEN CB_FA LIKE 'PKA' AND C_FA_FREIGABE LIKE 'J' AND C_ABT_ABSCHLUSS LIKE 'J' THEN 'Green'
        ELSE 'Grey'
    END AS PKA_Status,
    CASE
        WHEN CB_FA LIKE 'PSA' AND C_FA_FREIGABE LIKE 'J' AND C_ABT_ABSCHLUSS LIKE 'N' THEN 'Yellow'
        WHEN CB_FA LIKE 'PSA' AND C_FA_FREIGABE LIKE 'J' AND C_ABT_ABSCHLUSS LIKE 'J' THEN 'Green'
        ELSE 'Grey'
    END AS PSA_Status,
    CASE
        WHEN (CB_FA LIKE 'POR' OR CB_FA LIKE 'PRO' OR CB_FA LIKE 'PUCH' OR CB_FA LIKE 'ZOU') AND C_FA_FREIGABE LIKE 'J' AND C_ABT_ABSCHLUSS LIKE 'N' THEN 'Yellow'
        WHEN (CB_FA LIKE 'POR' OR CB_FA LIKE 'PRO' OR CB_FA LIKE 'PUCH' OR CB_FA LIKE 'ZOU') AND C_FA_FREIGABE LIKE 'J' AND C_ABT_ABSCHLUSS LIKE 'J' THEN 'Green'
        ELSE 'Grey'
    END AS ZOU_Status
FROM tblfall f 
LEFT JOIN tblvorgang v on f.fallid = v.fallid and v.vorgangstyp = 'GKH-Formular'
LEFT JOIN qck_abtabrechnung a on v.vorgangsid = a.vorgangsid
LEFT JOIN qck_g2abtabrechnung g on a.layoutdataid = g.layoutdataid
WHERE (sysdate - f.fallstartdatum) <= 1000; 

但是我希望sql结果只有一行,如果可能的话,它只需要灰色,所以像这样:

+----------+---------------+--------------+--------------+
| "FALLID" | "PGCH_STATUS" | "PKA_STATUS" | "ZOU_STATUS" |
+----------+---------------+--------------+--------------+
| "687099" | "Yellow"      | "Grey"       | "Yellow"     |
+----------+---------------+--------------+--------------+

我应该用什么方式?我是否需要另一个子查询,是否需要修改我的查询,或者根本没有办法?

提前致谢!

2 个答案:

答案 0 :(得分:4)

假设您的字段始终不为空,您可以使用以下内容包装视图:

with your_view(FALLID, PGCH_STATUS, PKA_STATUS, ZOU_STATUS) as
(
select '687099', 'Grey', 'Grey', 'Yellow' from dual union all
select '687099', 'Yellow', 'Grey', 'Grey' from dual
)
select FALLID,
       nvl( max( decode (PGCH_STATUS, 'Grey', null, PGCH_STATUS)), 'Grey') as PGCH_STATUS,
       nvl( max( decode (PKA_STATUS , 'Grey', null, PKA_STATUS )), 'Grey') as PKA_STATUS,
       nvl( max( decode (ZOU_STATUS , 'Grey', null, ZOU_STATUS )), 'Grey') as ZOU_STATUS
from your_view
group by FALLID

这将返回&#39;格雷&#39;只有当所有行都包含&#39; Gray&#39;在那一栏;否则它将返回与&#39; Gray&#39;不同的最大字符串。 在您的情况下,它将是:

select FALLID,
           nvl( max( decode (PGCH_STATUS, 'Grey', null, PGCH_STATUS)), 'Grey') as PGCH_STATUS,
           nvl( max( decode (PKA_STATUS , 'Grey', null, PKA_STATUS )), 'Grey') as PKA_STATUS,
           nvl( max( decode (ZOU_STATUS , 'Grey', null, ZOU_STATUS )), 'Grey') as ZOU_STATUS
from UDV_GKH_ABT_STATUS 
group by FALLID

答案 1 :(得分:1)

将视图放在查询中,而不是sample_data和WITH子句。

'http://stackoverflow.com/questions/35475103/how-can-i-get-a-url-from-chrome-by-python'

另一方面,您使用SQL> WITH sample_data(FALLID,PGCH_STATUS,PKA_STATUS,ZOU_STATUS) AS( 2 SELECT 687099 , 'Grey' , 'Grey' , 'Yellow' FROM dual UNION ALL 3 SELECT 687099 , 'Yellow', 'Grey' , 'Grey' FROM dual 4 ) 5 -- end of sample_data mimicking real table 6 SELECT MAX(fallid), 7 NVL(MAX( 8 CASE 9 WHEN PGCH_STATUS = 'Grey' 10 THEN '' 11 ELSE PGCH_STATUS 12 END), 'Grey') PGCH_STATUS, 13 NVL(MAX( 14 CASE 15 WHEN PKA_STATUS = 'Grey' 16 THEN '' 17 ELSE PKA_STATUS 18 END), 'Grey') PKA_STATUS, 19 NVL(MAX( 20 CASE 21 WHEN ZOU_STATUS = 'Grey' 22 THEN '' 23 ELSE ZOU_STATUS 24 END), 'Grey') ZOU_STATUS 25 FROM sample_data 26 GROUP BY fallid; MAX(FALLID) PGCH_STATUS PKA_STATUS ZOU_STATUS ----------- ----------- ----------- ----------- 687099 Yellow Grey Yellow SQL> 毫无意义:

LIKE

优化工具会将其评估为:

LIKE 'ECH'

虽然它对性能没有影响,因为优化器足够聪明,可以评估为= 'ECH'

您可以查看解释计划,

=