将行与多个id组合到涉及clob列的单行

时间:2016-03-04 12:12:15

标签: sql db2

我正在使用DB2数据库。我的问题是这个

我有这样的表

  ID    Msg1(CLob)  Msg2(Clob)  Msg3(Clob)

  1          SomeTex1          NULL       NULL
  1          NULL             SomeText2   NULL
  2          NULL             NULL        NULL
  2          SomeText3        SomeText4   NULL
  3          SomeText5         NULL       NULL
  3           NULL            NULL      SomeText6

我需要这样的结果

  ID         Msg1(CLob)       Msg2(Clob)      Msg3(Clob)
   1         SomeTex1         SomeText2      NULL
   2         SomeText3        SOmeText4      NULL
   3         SomeText5        NULL           Somtext6

基本上我需要按ID分组,但同时需要通过组合行来消除空值。

2 个答案:

答案 0 :(得分:0)

CLOB总是让事情变得更复杂。相反,使用VARCHAR避免它们可以选择更简单的解决方案。

如果LOB无法避免,因为你的尺寸确实比32kb大,这可能是解决问题的一个选择:

with temp as (
select id, msg1, msg2, msg3, 
       row_number() over (partition by id order by 1,2,3,4) as rownum
  from t3
)
select t1.id, 
       coalesce(t1.msg1, t2.msg1, t3.msg1) as msg1,
       coalesce(t1.msg2, t2.msg2, t3.msg2) as msg2,
       coalesce(t1.msg3, t2.msg3, t3.msg3) as msg3
 from temp t1 
 left join temp t2 on t1.id = t2.id and t1.rownum = 1 and t2.rownum=2
 left join temp t3 on t1.id = t3.id and t1.rownum = 1 and t3.rownum=3
where t1.rownum = 1

答案 1 :(得分:0)

以下SQL解决了这个问题:

select
    f1,
    listagg( nvl2(f2,f2,' ')) as f2,
    listagg( nvl2(f3,f3,' ')) as f3,
    listagg( nvl2(f4,f4,' ')) as f4
from t0
group by f1

使用DB2 LUW V10.5.3:

使用以下SQL脚本提供详细信息:

connect to pocdb;

drop table t0;
create table t0 (
    f1 integer,
    --f2 char(100),
    --f3 char(100),
    --f4 char(100)
    f2      clob,
    f3      clob,
    f4      clob
)
;

insert into t0(f1,f2) values (1,'msg - f2 - key 1');
insert into t0(f1,f3) values (1,'msg - f3 - key 1');
insert into t0(f1,f4) values (1,'msg - f4 - key 1');

insert into t0(f1,f2) values (2,'msg - f2 - key 2');
insert into t0(f1,f3) values (2,'msg - f3 - key 2');
insert into t0(f1,f4) values (2,'msg - f4 - key 2');

insert into t0(f1,f2) values (3,'msg - f2 - key 3');
insert into t0(f1,f3) values (3,'msg - f3 - key 3');

insert into t0(f1,f2) values (4,'msg - f2 - key 4');
insert into t0(f1,f4) values (4,'msg - f4 - key 4');

insert into t0(f1,f2) values (5,'msg - f2 - key 5');

insert into t0(f1,f3) values (6,'msg - f3 - key 6');
insert into t0(f1,f4) values (6,'msg - f4 - key 6');

export to /tmp/t0.unl of del
select
    f1,
    listagg( nvl2(f2,f2,' ')) as f2,
    listagg( nvl2(f3,f3,' ')) as f3,
    listagg( nvl2(f4,f4,' ')) as f4
from t0
group by f1
;

connect reset;
terminate;

运行命令:     db2 -tvf t0.sql> t0.out 2>& 1

t0.out 结果创建的是:

cat t0.out

connect to pocdb

  Database Connection Information

Database server        = DB2/LINUXX8664 10.5.3
SQL authorization ID   = DB2INST1
Local database alias   = POCDB


drop table t0
DB20000I  The SQL command completed successfully.

create table t0 ( f1 integer, f2        clob, f3        clob, f4        clob )
DB20000I  The SQL command completed successfully.

insert into t0(f1,f2) values (1,'msg - f2 - key 1')
DB20000I  The SQL command completed successfully.

insert into t0(f1,f3) values (1,'msg - f3 - key 1')
DB20000I  The SQL command completed successfully.

insert into t0(f1,f4) values (1,'msg - f4 - key 1')
DB20000I  The SQL command completed successfully.

insert into t0(f1,f2) values (2,'msg - f2 - key 2')
DB20000I  The SQL command completed successfully.

insert into t0(f1,f3) values (2,'msg - f3 - key 2')
DB20000I  The SQL command completed successfully.

insert into t0(f1,f4) values (2,'msg - f4 - key 2')
DB20000I  The SQL command completed successfully.

insert into t0(f1,f2) values (3,'msg - f2 - key 3')
DB20000I  The SQL command completed successfully.

insert into t0(f1,f3) values (3,'msg - f3 - key 3')
DB20000I  The SQL command completed successfully.

insert into t0(f1,f2) values (4,'msg - f2 - key 4')
DB20000I  The SQL command completed successfully.

insert into t0(f1,f4) values (4,'msg - f4 - key 4')
DB20000I  The SQL command completed successfully.

insert into t0(f1,f2) values (5,'msg - f2 - key 5')
DB20000I  The SQL command completed successfully.

insert into t0(f1,f3) values (6,'msg - f3 - key 6')
DB20000I  The SQL command completed successfully.

insert into t0(f1,f4) values (6,'msg - f4 - key 6')
DB20000I  The SQL command completed successfully.

export to /tmp/t0.unl of del select f1, listagg( nvl2(f2,f2,' ')) as f2,
   listagg( nvl2(f3,f3,' ')) as f3, listagg( nvl2(f4,f4,' ')) as f4 
   from t0 group by f1
SQL3104N  The Export utility is beginning to export data to file
"/tmp/t0.unl".

SQL3105N  The Export utility has finished exporting "6" rows.


Number of rows exported: 6


connect reset
DB20000I  The SQL command completed successfully.

terminate
DB20000I  The TERMINATE command completed successfully.

创建的导出结果是:

cat /tmp/t0.unl

1,"msg - f2 - key 1  "," msg - f3 - key 1 ","  msg - f4 - key 1"
2,"msg - f2 - key 2  "," msg - f3 - key 2 ","  msg - f4 - key 2"
3,"msg - f2 - key 3 "," msg - f3 - key 3","  "
4,"msg - f2 - key 4 ","  "," msg - f4 - key 4"
5,"msg - f2 - key 5"," "," "
6,"  ","msg - f3 - key 6 "," msg - f4 - key 6"