0

我有一个表,其中包含我想连接的行。我需要按 ID1、ID2 分组,并根据行号将每个 T 类型与父 A 类型连接起来。

这就是我所拥有的:

with t as (
    select '123' as ID1, '0' as ID2, 1500 as LINE, 'A' as type, 'First line ' as txt from sysibm.sysdummy1
    union all
    select '123' as ID1, '0' as ID2, 1510 as LINE, 'T' as type, 'It' as txt from sysibm.sysdummy1
    union all
    select '123' as ID1, '0' as ID2, 1520 as LINE, 'T' as type, 'works' as txt from sysibm.sysdummy1
    union all
    select '123' as ID1, '0' as ID2, 1530 as LINE, 'T' as type, 'very' as txt from sysibm.sysdummy1
    union all
    select '123' as ID1, '0' as ID2, 1540 as LINE, 'T' as type, 'well !' as txt from sysibm.sysdummy1
    union all
    select '123' as ID1, '0' as ID2, 2000 as LINE, 'A' as type, 'Second line' as txt from sysibm.sysdummy1
    union all
    select '123' as ID1, '0' as ID2, 2010 as LINE, 'T' as type, 'i am' as txt from sysibm.sysdummy1
    union all
    select '123' as ID1, '0' as ID2, 2020 as LINE, 'T' as type, 'happy.' as txt from sysibm.sysdummy1
    union all
    select '123' as ID1, '1' as ID2, 1500 as LINE, 'A' as type, 'Another line' as txt from sysibm.sysdummy1
    union all
    select '123' as ID1, '1' as ID2, 1510 as LINE, 'T' as type, 'It''s' as txt from sysibm.sysdummy1
    union all
    select '123' as ID1, '1' as ID2, 1520 as LINE, 'T' as type, 'a' as txt from sysibm.sysdummy1
    union all
    select '123' as ID1, '1' as ID2, 1530 as LINE, 'T' as type, 'pleasure' as txt from sysibm.sysdummy1
    union all
    select '123' as ID1, '1' as ID2, 1540 as LINE, 'T' as type, 'to' as txt from sysibm.sysdummy1
    union all
    select '123' as ID1, '1' as ID2, 1550 as LINE, 'T' as type, 'read' as txt from sysibm.sysdummy1
    union all
    select '123' as ID1, '1' as ID2, 1560 as LINE, 'T' as type, '!' as txt from sysibm.sysdummy1
    union all
    select '123' as ID1, '1' as ID2, 2100 as LINE, 'A' as type, 'Line without details' as txt from sysibm.sysdummy1
    union all
    select '456' as ID1, '0' as ID2, 1500 as LINE, 'A' as type, 'This is not the same id' as txt from sysibm.sysdummy1
)
select *
from t

这就是我要的 :

with t as (
select '123' as ID1, '0' as ID2, 1500 as LINE, 'First line It works very well !' as txt from sysibm.sysdummy1
union all
select '123' as ID1, '0' as ID2, 2000 as LINE, 'Second line i am happy.' as txt from sysibm.sysdummy1
union all
select '123' as ID1, '1' as ID2, 1500 as LINE, 'Another line It''s a pleasure to read !' as txt from sysibm.sysdummy1
union all
select '123' as ID1, '1' as ID2, 2100 as LINE, 'Line without details' as txt from sysibm.sysdummy1
union all
select '456' as ID1, '0' as ID2, 1500 as LINE, 'This is not the same id' as txt from sysibm.sysdummy1
)
select *
from t

你有想法吗 ?

谢谢,

4

2 回答 2

0

假设你想要一个表,而不是 SQL 语句,试试这个:

select id1,id2,line / 100 * 100, listagg(txt,' ') within group (order by type,line)
from   t
group  by id1,id2,line / 100
于 2021-07-27T16:08:37.400 回答
0

尝试这个:

/*
with t as (
...
)
*/
select 
  id1, id2
, min (case type when 'A' then line end) line
, listagg (txt, ' ') within group (order by type, line) txt
from
(
select 
  t.*
, sum (case type when 'A' then 1 else 0 end) over (partition by id1, id2 order by line) grp
from t
)
group by id1, id2, grp

grp我们在里面构造了一个人工群id1, id2。每次我们面对 type = 'A' withinid1, id2时,这个组号都会增加 1。

dbfiddle链接。

于 2021-07-28T08:25:14.250 回答