Hi scarletAni,
I haven't quite got the answer your looking for because I can't find a way to interpret the original sequence of your data, here's my solution...
Using the following as test data explains the problem with original sequence better -
create multiset table tt_table (
col1 char(1),
col2 char(6))
primary index (col1);
insert into tt_table ('A','The');
insert into tt_table ('A','cat');
insert into tt_table ('A','sat');
insert into tt_table ('A','on ');
insert into tt_table ('A','the');
insert into tt_table ('A','mat');
insert into tt_table ('B','Mary');
insert into tt_table ('B','had');
insert into tt_table ('B','a ');
insert into tt_table ('B','little');
insert into tt_table ('B','lamb');
Then the following SQL -
SELECT tt1.col1 || ' ' ||
trim(case when tt1.col2 is null then ' ' else tt1.col2 end) || ' ' ||
trim(case when tt2.col2 is null then ' ' else tt2.col2 end) || ' ' ||
trim(case when tt3.col2 is null then ' ' else tt3.col2 end) || ' ' ||
trim(case when tt4.col2 is null then ' ' else tt4.col2 end) || ' ' ||
trim(case when tt5.col2 is null then ' ' else tt5.col2 end) || ' ' ||
trim(case when tt6.col2 is null then ' ' else tt6.col2 end) colzzz
FROM (select col1, col2
from (SELECT col1, col2, rank(col2, col1) col3 FROM tt_table GROUP BY 1) ttx1
where col3=1) tt1
LEFT JOIN
(select col1, col2
from (SELECT col1, col2, rank(col2, col1) col3 FROM tt_table GROUP BY 1) ttx2
where col3=2) tt2
ON tt1.col1=tt2.col1
LEFT JOIN
(select col1, col2
from (SELECT col1, col2, rank(col2, col1) col3 FROM tt_table GROUP BY 1) ttx3
where col3=3) tt3
ON tt1.col1=tt3.col1
LEFT JOIN
(select col1, col2
from (SELECT col1, col2, rank(col2, col1) col3 FROM tt_table GROUP BY 1) ttx4
where col3=4) tt4
ON tt1.col1=tt4.col1
LEFT JOIN
(select col1, col2
from (SELECT col1, col2, rank(col2, col1) col3 FROM tt_table GROUP BY 1) ttx5
where col3=5) tt5
ON tt1.col1=tt5.col1
LEFT JOIN
(select col1, col2
from (SELECT col1, col2, rank(col2, col1) col3 FROM tt_table GROUP BY 1) ttx6
where col3=6) tt6
ON tt1.col1=tt6.col1
LEFT JOIN
(select col1, col2
from (SELECT col1, col2, rank(col2, col1) col3 FROM tt_table GROUP BY 1) ttx7
where col3=7) tt7
ON tt1.col1=tt7.col1;
Gives -
colzzz
B Mary little lamb had a
A the sat on mat cat
A The sat on mat cat
I hope that helps!
Roger...