DB2 for i can do this.
Perhaps the most concise way is to use an often overlooked feature, that the other DB2 platforms do not have -- a type of recursive query known as a hierarchical query. Like many things, it's fairly simple once you understand it, but it deserves a bit of explaining to understand how it works.
A recursive or hierarchical query helps you connect one row to one or more others. In this case we are making a chain of one to one connections.
The aggregate SYS_CONNECT_BY_PATH function will concatenate strings along the path from a starting (or "root") row to the end (or "leaf") row.
Let's start by putting your sample data into a table.
declare global temporary table snippets
( item varchar(10)
, seq smallint
, words varchar(30)
);
insert into snippets
values
('12755', 1, 'this item')
,('12755', 2, 'is no longer')
,('12755', 3, 'for sale')
,('abc123', 1, 'please use')
,('abc123', 2, 'another code')
,('xyz987', 1, 'obsolete')
;
Let's look at the hierarchical query we will use as a first phase building block.
SELECT item
, seq
, CONNECT_BY_ISLEAF as flag
, SYS_CONNECT_BY_PATH(words, ' ') as phrase
FROM snippets
START WITH seq = 1
CONNECT BY PRIOR item = item and PRIOR (seq + 1) = seq
The CONNECT BY clause defines the join conditions from one row in the table to another.
The START WITH clause specifies which row(s) to begin joining from, the "root" row. So we are going to start with rows where seq=1, and join to the next seq value (seq+1). That row will join to the following seq value, and so on.
CONNECT_BY_ISLEAF will indicate when we are at the last row in the chain. You'll in a moment why I used it here.
Here are the results:
ITEM SEQ FLAG PHRASE
12755 1 0 this item
12755 2 0 this item is no longer
12755 3 1 this item is no longer for sale
abc123 1 0 please use
abc123 2 1 please use another code
xyz987 1 1 obsolete
Now all we need to do is put that in a common table expression,
from which we can take only the rows we want. The "leaf" rows at the end of the chain have the completed string, so those are the ones to pick.
with q as
(
SELECT item
, seq
, CONNECT_BY_ISLEAF as flag
, SYS_CONNECT_BY_PATH(words, ' ') as phrase
FROM snippets
START WITH seq = 1
CONNECT BY PRIOR item = item and PRIOR (seq + 1) = seq
)
SELECT item
, phrase
FROM q
WHERE flag = 1
ORDER BY item
;
Which gives us:
ITEM PHRASE
abc123 please use another code
xyz987 obsolete
12755 this item is no longer for sale
There you have it.
Folks on other DB2 platforms will have to find another solution, such as a regular recursive query in funkworm's comment above.
GROUP_CONCAT
but it appears that DB2 doesn't have that. Some Googling gave me this article on an alternative forGROUP_CONCAT
for DB2 that I think might be what you need: radheshk.blogspot.nl/2008/02/… You'll have to work in anORDER BY Textline
yourself :)