将多个值从列连接到行,并使用CASE_WHEN进行过滤

时间:2019-07-16 11:28:49

标签: oracle stored-procedures case-when

我必须对字段中每一行的可能值进行描述,并希望根据一些案例研究显示两个不同的术语:

SELECT DISTINCT CODE,
    DENOM, 
    MODIFY,
    CASE WHEN (S_ABILIT = 'ABILITATO' AND MODIFY = 1) 
        THEN listagg(SHORTDE, ' - ') WITHIN GROUP (ORDER BY SHORTDE)
        WHEN (MODIFY = 0)
        THEN 'Nessuno'
        WHEN (S_ABILIT = 'DISABILITATO' AND MODIFY = 1) 
        THEN 'Tutti'
    END AS PRFABI
FROM ( 
  SELECT DISTINCT A.CODE, C.SHORTDE, B.DENOM, A.DINI, A.DFIN,
    CASE 
        WHEN (A.CODE NOT IN (SELECT DISTINCT code FROM fr_val_fa)) 
        THEN 1 
        ELSE 0 
    END AS MODIFY, 
    CASE 
        WHEN SYSDATE BETWEEN A.DINI AND A.DFIN 
        THEN 'ABILITATO' 
        ELSE 'DISABILITATO' 
    END AS S_ABILIT 
  FROM FR_VAL_PROF A 
  INNER JOIN PUN B  
    ON A.CODE= B.CODE
  INNER JOIN DANACARDPRFCFR C 
    ON A.CODPRF = C.CARDPRFCFR
     ) 

GROUP BY CODE, DENOM, MODIFY, S_ABILIT
ORDER BY CODE;

我的结果是:

CODE       |  DENOM             | MODIFY      |   PRFABI
----------------------------------------------------------------------
1          | Rome               |   1         |  Nessuno
3          | Milan              |   1         |  Green - Red 
2          | Florence           |   0         |  Tutti
3          | Milan              |   1         |  Nessuno  
4          | Naples             |   1         |  Green - Red - Orange - Yellow

但是我想要

CODE       |  DENOM             | MODIFY      |   PRFABI
----------------------------------------------------------------------
1          | Rome               |   1         |  Nessuno
3          | Milan              |   1         |  Green - Red 
2          | Florence           |   0         |  Tutti
4          | Naples             |   1         |  Green - Red - Orange - Yellow

我不想为未启用配置文件的代码显示新行

0 个答案:

没有答案