簡體   English   中英

何時返回多個值的情況

[英]Case when to return multiple values

這是我的表格,在日常情況下,我將使用解碼來顯示以下值,但是,我需要在特定報告的1列中顯示所有值,因為它們對應於同一項目。

T1:

Id V1 V2 V3 V4 V5 V5_Text   
1  1  1  1  0  0   Other    
2  0  1  1  0  1   Other     
3  0  0  1  0  1   QWE    
4  0  0  1  1  0   ABC    
5  1  0  0  0  1   Other 

我使用的情況是,但似乎無法返回超過1的值。

select id,
case when V1=1, then 'A'
     when V2=1, then 'B'
     when V3=1, then 'C'
     when V4=1, then 'D'
     when V5=1, then v5_text

預期產量:

1  A,B,C,Other   
2  B,C,Other    
3  C,QWE    
4  C,D,ABC   
5  A,Other  

實際輸出:

1  Other    
2  Other    
3  QWE     
4  ABC    
5  Other   

您需要串聯這些值:

select id,
       trim(',' from
            (case when V1 = 1 then 'A,' end) ||
             case when V2 = 1 then 'B,' end) ||
             case when V3 = 1 then 'C,' end) ||
             case when V4 = 1 then 'D,' end) ||
             case when V5 = 1 then v5_text
            )
           )

不知道您還有什么想法,也不知道這個表有多大,但是另一種方法是“取消透視”該v1 ... v5結構,然后使用listagg()

順便說一句:第一行

Id V1 V2 V3 V4 V5 V5_Text   
1  1  1  1  0  0   Other   
   A  B  C                   -- there is no "other" here

SQL小提琴

Oracle 11g R2架構設置

CREATE TABLE Table1
    (ID int, V1 int, V2 int, V3 int, V4 int, V5 int, V5_TEXT varchar2(5))
;

INSERT ALL 
    INTO Table1 (ID, V1, V2, V3, V4, V5, V5_TEXT)
         VALUES (1, 1, 1, 1, 0, 0, 'Other')
    INTO Table1 (ID, V1, V2, V3, V4, V5, V5_TEXT)
         VALUES (2, 0, 1, 1, 0, 1, 'Other')
    INTO Table1 (ID, V1, V2, V3, V4, V5, V5_TEXT)
         VALUES (3, 0, 0, 1, 0, 1, 'QWE')
    INTO Table1 (ID, V1, V2, V3, V4, V5, V5_TEXT)
         VALUES (4, 0, 0, 1, 1, 0, 'ABC')
    INTO Table1 (ID, V1, V2, V3, V4, V5, V5_TEXT)
         VALUES (5, 1, 0, 0, 0, 1, 'Other')
SELECT * FROM dual
;

查詢1

select
      id, listagg(alpha,',') within group (order by alpha) codes_concat
from (
    select id, v1, V5_TEXT, 'A' alpha from table1 where v1 = 1 union all
    select id, v2, V5_TEXT, 'B' alpha from table1 where v2 = 1 union all
    select id, v3, V5_TEXT, 'C' alpha from table1 where v3 = 1 union all
    select id, v4, V5_TEXT, 'D' alpha from table1 where v4 = 1 union all
    select id, v5, V5_TEXT, 'Other'   from table1 where v5 = 1 
    ) d
group by
      id

結果

| ID | CODES_CONCAT |
|----|--------------|
|  1 |        A,B,C |
|  2 |    B,C,Other |
|  3 |      C,Other |
|  4 |          C,D |
|  5 |      A,Other |

編輯

如果工會冒犯了所有人,那么還有其他方法:

select
      id, listagg(alpha,',') within group (order by alpha) codes_concat
from (
      select 
            id
          , V5_TEXT
          , case when cj.n = 1 then v1
                 when cj.n = 2 then v2
                 when cj.n = 3 then v3
                 when cj.n = 4 then v4
                 when cj.n = 5 then v5 end v1
          , case when cj.n = 1 then 'A'
                 when cj.n = 2 then 'B'
                 when cj.n = 3 then 'C'
                 when cj.n = 4 then 'D'
                 when cj.n = 5 then V5_TEXT end alpha
      from table1
      cross join (
          Select level n from dual connect by level<=5
          ) cj
    ) d
where v1 <> 0
group by
      id

;

或可以使用樞軸等。

select
      id, listagg(alpha,',') within group (order by alpha) codes_concat
from (
      select
            id, v5_text, v1
          , case when vname = 'V1' then 'A'
                 when vname = 'V2' then 'B'
                 when vname = 'V3' then 'C'
                 when vname = 'V4' then 'D'
                 when vname = 'V5' then v5_text end alpha
      from table1
      UNPIVOT ( 
              v1 FOR( vname ) IN ( v1, v2, v3, v4, v5
              )
          )
    ) d
where v1 <> 0
group by
      id
;

希望對您有幫助,我用您的原始數據測試了此代碼

select id,
rtrim((case v1 when 1 then 'A,' else '' end ||
case v2   when 1 then 'B,' else '' end ||
case v3   when 1 then 'C,' else '' end ||
case  v4  when 1 then 'D,' else '' end ||
case  v5 when 1 then v5_text || ',' else '' end),',')
from table1

結果的形象

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM