将行合并为 1 列

发布于 2024-11-14 18:50:32 字数 309 浏览 4 评论 0原文

我有一个包含 2 列的表,

Input
Col 1 ---- Col 2
1     ---- aaaa  
1     ---- bbbb
1     ---- cccc
2     ---- dddd
2     ---- eeee
2     ---- ffff
2     ---- gggg

Output

Col 1 ---- Col 2
1     ---- aaaabbbbcccc
2     ---- ddddeeeeffffgggg

我正在考虑进行几次自连接,但似乎效率不高。关于如何编写 sql 有什么想法吗?

I have a table with 2 columns

Input
Col 1 ---- Col 2
1     ---- aaaa  
1     ---- bbbb
1     ---- cccc
2     ---- dddd
2     ---- eeee
2     ---- ffff
2     ---- gggg

Output

Col 1 ---- Col 2
1     ---- aaaabbbbcccc
2     ---- ddddeeeeffffgggg

I was thinking of doing several self joins, but doesnt seem efficient. Any ideas on how the sql has to be written?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

属性 2024-11-21 18:50:32

好吧,我会咬的。而不是 stragg,尝试 listagg (在 11.2 中):

create table tst1
(
pid number,
val varchar2(10)
);


insert into tst1 values(1, 'Rec1');
insert into tst1 values(1, 'Rec2');
insert into tst1 values(1, 'Rec3');
insert into tst1 values(2, 'Rec1');
insert into tst1 values(2, 'Rec2');
commit;


select pid, listagg(val, ':') within group(order by val) as "The List"
from tst1
group by pid;

你会得到:

pid   The List
1     Rec1:Rec2:Rec3
2     Rec1:Rec2

如果你将 order by 更改为“order by val desc”,你会得到

pid   The List
1     Rec3:Rec2:Rec1
2     Rec2:Rec1

Ok, I'll bite. Instead of stragg, try listagg (in 11.2):

create table tst1
(
pid number,
val varchar2(10)
);


insert into tst1 values(1, 'Rec1');
insert into tst1 values(1, 'Rec2');
insert into tst1 values(1, 'Rec3');
insert into tst1 values(2, 'Rec1');
insert into tst1 values(2, 'Rec2');
commit;


select pid, listagg(val, ':') within group(order by val) as "The List"
from tst1
group by pid;

And you get:

pid   The List
1     Rec1:Rec2:Rec3
2     Rec1:Rec2

If you change the order by to "order by val desc" you'd get

pid   The List
1     Rec3:Rec2:Rec1
2     Rec2:Rec1
厌味 2024-11-21 18:50:32

该版本适用于Oracle 9i 及更高版本。

create table foo (
  key_column number,
  val_column varchar2(4)
);

insert into foo values (1, 'aaaa');
insert into foo values (1, 'bbbb');
insert into foo values (1, 'cccc');
insert into foo values (2, 'dddd');
insert into foo values (2, 'eeee');
insert into foo values (2, 'ffff');
insert into foo values (2, 'gggg');


    select key_column
         , replace(max(sys_connect_by_path(val_column, ',')), ',') combined
      from (select key_column
                 , val_column
                 , row_number() over (partition by key_column order by val_column) cur
                 , row_number() over (partition by key_column order by val_column) - 1 prev
              from foo) foo
  group by key_column 
connect by prior cur = prev and prior key_column = key_column
start with cur = 1;

   key_column | val_column
  --------------------------
            1 | aaaabbbbcccc
            2 | ddddeeeeffffgggg

This is a version that will work in Oracle 9i and up.

create table foo (
  key_column number,
  val_column varchar2(4)
);

insert into foo values (1, 'aaaa');
insert into foo values (1, 'bbbb');
insert into foo values (1, 'cccc');
insert into foo values (2, 'dddd');
insert into foo values (2, 'eeee');
insert into foo values (2, 'ffff');
insert into foo values (2, 'gggg');


    select key_column
         , replace(max(sys_connect_by_path(val_column, ',')), ',') combined
      from (select key_column
                 , val_column
                 , row_number() over (partition by key_column order by val_column) cur
                 , row_number() over (partition by key_column order by val_column) - 1 prev
              from foo) foo
  group by key_column 
connect by prior cur = prev and prior key_column = key_column
start with cur = 1;

   key_column | val_column
  --------------------------
            1 | aaaabbbbcccc
            2 | ddddeeeeffffgggg
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文