db2合并多行数据

这就是一个行转换成列问题。

我给个例子,你参考一下:
数据是这样的:
A   B    C   D   E
-------------------------
1   5    3   4   43米
2   0    0   0   53立方
2   0    34  0   136克
3   0    0   0   1斤
...


我想将第一列(A)进行分组,然后 如果第一列 相同的,最后一列(E)变为一行数据 ,并以,隔开。
最后形成的数据是这样  
转成:
A    F
---------------------------------------
2    53立方,136克


====================================================================

数据准备:
create table t1 (
  a   int
, b int
, c int  
, d  varchar(20)
);

insert into t1(a,d) values(1,'x');
insert into t1(a,d) values(1,'y');
insert into t1(a,d) values(2,'abc');
insert into t1(a,d) values(2,'spsoft');
insert into t1(a,d) values(2,'gift');
insert into t1(a,d) values(3,' ');
insert into t1(a,d) values(3,'dd');
insert into t1(a,d) values(3,'ee');
insert into t1(a,d) values(3,'ff');

--select * from t1;
SQL code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
postgres=#
postgres=#  select  from  t1;
  a | b | c |   d
---+---+---+--------
  1 |   |   | x
  1 |   |   | y
  2 |   |   | abc
  2 |   |   | spsoft
  2 |   |   | gift
  3 |   |   |
  3 |   |   | dd
  3 |   |   | ee
  3 |   |   | ff
(9 行记录)
 
postgres=#
postgres=#
postgres=#
postgres-#
postgres-#    select  row_number() over(partition  by  order  by  d) rn   -- 组内行号
postgres-#        ,  count (*) over(partition  by  a) cnt                 -- 组内记录数
postgres-#        , t.*
postgres-#  from  t1 t
postgres-# ;
  rn | cnt | a | b | c |   d
----+-----+---+---+---+--------
   1 |   2 | 1 |   |   | x
   2 |   2 | 1 |   |   | y
   1 |   3 | 2 |   |   | abc
   2 |   3 | 2 |   |   | gift
   3 |   3 | 2 |   |   | spsoft
   1 |   4 | 3 |   |   |
   2 |   4 | 3 |   |   | dd
   3 |   4 | 3 |   |   | ee
   4 |   4 | 3 |   |   | ff
(9 行记录)
 
 
postgres=#
postgres=#
postgres=#
postgres=#  select  a
postgres-#      ,  case  cnt
postgres-#              when  then  c1
postgres-#              when  then  c1|| ',' ||c2
postgres-#              when  then  c1|| ',' ||c2|| ',' ||c3
postgres-#              when  then  c1|| ',' ||c2|| ',' ||c3|| ',' ||c4
postgres-#              when  then  c1|| ',' ||c2|| ',' ||c3|| ',' ||c4|| ',' ||c5
postgres-#         end  str1
postgres-#   from  (
postgres(#  select  a
postgres(#      , cnt
postgres(#      ,  max ( case  rn  when  then  end   ) c1
postgres(#      ,  max ( case  rn  when  then  end   ) c2
postgres(#      ,  max ( case  rn  when  then  end   ) c3
postgres(#      ,  max ( case  rn  when  then  end   ) c4
postgres(#      ,  max ( case  rn  when  then  end   ) c5
postgres(#    from  (
postgres(#          select  row_number() over(partition  by  order  by   d) rn, count (*) over(partition  by  a ) cnt,t.*  from  t1 t
postgres(#         ) t
postgres(#   group  by  a,cnt
postgres(#  ) t
postgres-#   order  by  1
postgres-# ;
  a |      str1
---+-----------------
  1 | x,y
  2 | abc,gift,spsoft
  3 |  ,dd,ee,ff
(3 行记录)
 
 
postgres=#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值