这就是一个行转换成列问题。
我给个例子,你参考一下:
数据是这样的:
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;
我给个例子,你参考一下:
数据是这样的:
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;
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
a
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
1
then
c1
postgres-#
when
2
then
c1||
','
||c2
postgres-#
when
3
then
c1||
','
||c2||
','
||c3
postgres-#
when
4
then
c1||
','
||c2||
','
||c3||
','
||c4
postgres-#
when
5
then
c1||
','
||c2||
','
||c3||
','
||c4||
','
||c5
postgres-#
end
str1
postgres-#
from
(
postgres(#
select
a
postgres(# , cnt
postgres(# ,
max
(
case
rn
when
1
then
d
end
) c1
postgres(# ,
max
(
case
rn
when
2
then
d
end
) c2
postgres(# ,
max
(
case
rn
when
3
then
d
end
) c3
postgres(# ,
max
(
case
rn
when
4
then
d
end
) c4
postgres(# ,
max
(
case
rn
when
5
then
d
end
) c5
postgres(#
from
(
postgres(#
select
row_number() over(partition
by
a
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=#
|