如何使用coalesce打印列名和值

时间:2015-08-18 13:27:15

标签: sql oracle plsql

我需要在表的每一行中打印第一个非空列的值。我正在使用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

请帮帮我。

2 个答案:

答案 0 :(得分: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()执行此操作,但这种简单的方法在Oracle中不起作用。所以,这不符合你的要求:

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

问题是Oracle将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;

答案 1 :(得分: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

N.B。我假设你的行有某种唯一的标识符,加上一些列来排序行(在我使用的样本数据中,我使用的是与我用作唯一标识符的列相同)

相关问题