This query tested by me today.
Same result, online, cursor dosn't need.
You can use it, if you want.
with Persons (person_id, v) as
(
select varchar('0001', 50), varchar('A1', 50)
from sysibm.sysdummy1
union all
select '0001', 'A3'
from sysibm.sysdummy1
union all
select '0002', 'A1'
from...
Select p1.person_id,
Min('(' || p1.v
|| ifnull(', ' || p2.v, '')
|| ifnull(', ' || p3.v, '') || ')') as V
from persons p1 left join persons p2
On p1.person_id = p2.person_id
and p2.v > p1.v
left join persons p3
On p2.person_id = p3.person_id
and p3.v > p2.v
Group by...
Any problem has dozens solutions.
Programmer has to find the best way to solve problem.
One more solution.
If you know depth for person (in our case could be 3) you can use very simple query:
Select p1.person_id,
'(' || p1.v
|| ifnull(', ' || p2.v, '')
|| ifnull(', ' || p3.v, '') || ')'...
Hi Larry !
Any client application manipulate the data faster then DB engine.
But anyway you have to declare cursor inside of the application and fetch rows one by one.
Therefore you can't make program without DB engine.
But if you want to show the result immediately, you have no choice...
Correction:
with Person_in (person_id, v) as
(
select varchar('0001', 50), varchar('A1', 50)
from sysibm.sysdummy1
union all
select '0001', 'A2'
from sysibm.sysdummy1
union all
select '0001', 'A3'
from sysibm.sysdummy1
union all
select '0002', 'A1'
from sysibm.sysdummy1
union all
select...
In case if we have DB2 V9, we can use function rowno over() and Order by in Person_Seq step.
If we have duplicates and Nulls we have to add Person_dst and change Person_Seq step.
See bellow:
with Person_in (person_id, v) as
(
select varchar('0001', 50), varchar('A1', 50)
from...
Hi Ties Blom !
How I know this is most generic solution for this kind of task.
If you have duplicates for this two columns, or Nulls, it could make solution a bit different, but you have to think in same way.
Lenny
This is solution. How I promissed.
with Person_in (person_id, v) as
(
select varchar('0001', 50), varchar('A1', 50)
from sysibm.sysdummy1
union all
select '0001', 'A2'
from sysibm.sysdummy1
union all
select '0001', 'A3'
from sysibm.sysdummy1
union all
select '0002', 'A1'
from...
Dear LarrySteele !
For solving your problem you have to use recursive common table expression technique.
This is the best way to solve your problem.
I can create this query to you, but would be much better if you will learn about this before...
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.