2015-08-18 30 views
2

我需要在表的每一行中打印第一個非空列的值。我爲此使用了COALESCE。但是我想打印列的名稱和值。如何使用合併功能打印列名以及值

我的表看起來像這樣:

COL1   | COL2  | COL3  |COL4 
******************************************************** 
      |Value of col2|    |Value of col4 
--------------------------------------------------------- 
      |    |Value of col3|Value of col4 
---------------------------------------------------------    
Value of col1|    |    |Value of col4 
--------------------------------------------------------- 
      |    |    | 
********************************************************* 

我的代碼是這樣的:

declare 
    v_count integer; 
    v_counter integer; 
    cursor c is 
    select rowid, coalesce(col1,col2,col3,col4) not_null_value 
     from handson_table; 
begin 
    select count(*) into v_count from handson_table; 

    dbms_output.put_line('There are '||v_count||' rows'); 

    v_counter:=1; 

    for r in c 
    loop 
    dbms_output.put_line(rpad('*',5,'*') 
         || 'Row Number' || v_counter 
         || rpad('*',5,'*')); 

    if (length(r.not_null_value) > 1) then 
     dbms_output.put_line(r.not_null_value); 
    else 
     dbms_output.put_line('All columns of this row is NULL'); 
    end if; 

    v_counter:=v_counter+1; 
    end loop; 
end; 

OUTPUT就這樣產生了:

There are 4 rows 
*****Row Number 1***** 
Value of col2 
*****Row Number 2***** 
Value of col3 
*****Row Number 3***** 
Value of col1 
*****Row Number 4***** 
All columns of this row is NULL 

我要像輸出:

There are 4 rows 
*****Row Number 1***** 
col2 : Value of col2 
*****Row Number 2***** 
col3 : Value of col3 
*****Row Number 3***** 
col1 : Value of col1 
*****Row Number 4***** 
All columns of this row is NULL 

請幫我一把。

回答

3

這是你想要的東西:

select (case when col1 is not null then 'col1 : ' || col1 
      when col2 is not null then 'col2 : ' || col2 
      when col3 is not null then 'col3 : ' || col3 
      when col4 is not null then 'col4 : ' || col4 
      else 'All Null' 
     end) 
from handson_table; 

編輯:

是的,你可以用COALESCE()做到這一點,但簡單的方法不會在甲骨文工作。所以,這不會做你想要什麼:

select COALESCE('col1 : ' || col1, 
       'col2 : ' || col2, 
       'col3 : ' || col3, 
       'col4 : ' || col4, 
       'All Null') 
from handson_table; 

的問題是,甲骨文把NULL作爲CONCAT一個空字符串。所以,你最終得到了類似於

如果您可以使用單個查詢,則不需要PL/SQL代碼塊。

select COALESCE(NULLIF('col1 : ' || col1, 'col1: '), 
       NULLIF('col2 : ' || col2, 'col2: '), 
       NULLIF('col3 : ' || col3, 'col3: '), 
       NULLIF('col4 : ' || col4, 'col4: '), 
       'All Null') 
from handson_table; 
+0

真的找到你的解決方案很棒這樣做與案件確實照顧它。但我有一個問題,無論如何,這與coalesce做到這一點。我只是出於好奇而問,否則你的解決方案就是爆炸性的! –

1

如果我是你,我會做它像這樣:

with sample_data as (select 10 id, null col1, 'value of col2' col2, null col3, 'value of col4' col4 from dual union all 
        select 20 id, null col1, null col2, 'value of col3' col3, 'value of col4' col4 from dual union all 
        select 30 id, 'value of col1' col1, null col2, null col3, 'value of col4' col4 from dual union all 
        select 40 id, null col1, null col2, null col3, null col4 from dual) 
select id, 
     row_number() over (order by id) rn, 
     case when col1 is not null then 'col1' 
      when col2 is not null then 'col2' 
      when col3 is not null then 'col3' 
      when col4 is not null then 'col4' 
     end name_of_col, 
     coalesce(col1, col2, col3, col4) col_value 
from sample_data; 

     ID   RN NAME_OF_COL COL_VALUE  
---------- ---------- ----------- ------------- 
     10   1 col2  value of col2 
     20   2 col3  value of col3 
     30   3 col1  value of col1 
     40   4 

注:我假定你的行有一些獨特的標識符,加上一些列來做排序(這在我使用的樣本數據是與唯一標識符相同的列)

+0

其實在我的行中沒有唯一的標識符,我也不能添加列。 –