我有一个表,其中包含我想concat的行。我需要按ID1、ID2分组,并根据行号将每个T类型与父A类型concat。
这是我的一切:
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
你有主意吗?
谢谢你,
2条答案
按热度按时间ct3nt3jp1#
假设您需要的是表,而不是SQL语句,请尝试以下操作:
xpcnnkqh2#
试试看:
我们在
id1, id2
中构造一个人工群grp
。每次我们在id1, id2
中面对type = 'A',这个群的编号就加1。dbfiddle链接。