Hi Rafi,
Yes, as Nishant pointed out an approach using recursive queries would be a more generic one
here is an eaxmple-
Assuming tb1 is the table containing data we need to make tb3 to rank the columns and then we can
use recrsive query to come up with desired output using recursive queries we need not have the prior
knowledge of the number of names realted to each id it could be varying like 2, or 3
create multiset volatile table tb1
(id integer
,nm varchar(5)
) primary index (id,nm) on commit preserve rows;
insert into tb1 values (10,'xy');
insert into tb1 values (10,'yz');
insert into tb1 values (10,'zx');
insert into tb1 values (20,'ab');
insert into tb1 values (20,'bc');
create multiset volatile table tb3
as
(
sel
id
,nm
,rank () over (partition by id order by nm) rn1
from
tb1
) with data primary index (id,nm) on commit preserve rows;
with recursive rslt (id,nm,rn1,lvl)
as(
sel
id,
cast ( nm as varchar(20)),
rn1,
0 as lvl
from
tb3
where
rn1 = 1
union all
sel
rslt.id,
rslt.nm || b.nm,
b.rn1,
rslt.lvl +1 as lvl
from
tb3 b
inner join
rslt
on
rslt.id = b.id
where
rslt.rn1 < b.rn1
)
sel id,nm
from rslt
qualify rank() over (partition by id order by lvl desc ) = 1;
Regards,
R.Rajeev
Hi Rafi,
Yes, as Nishant pointed out an approach using recursive queries would be a more generic one
here is an eaxmple-
Assuming tb1 is the table containing data we need to make tb3 to rank the columns and then we can
use recrsive query to come up with desired output using recursive queries we need not have the prior
knowledge of the number of names realted to each id it could be varying like 2, or 3
create multiset volatile table tb1
(id integer
,nm varchar(5)
) primary index (id,nm) on commit preserve rows;
insert into tb1 values (10,'xy');
insert into tb1 values (10,'yz');
insert into tb1 values (10,'zx');
insert into tb1 values (20,'ab');
insert into tb1 values (20,'bc');
create multiset volatile table tb3
as
(
sel
id
,nm
,rank () over (partition by id order by nm) rn1
from
tb1
) with data primary index (id,nm) on commit preserve rows;
with recursive rslt (id,nm,rn1,lvl)
as(
sel
id,
cast ( nm as varchar(20)),
rn1,
0 as lvl
from
tb3
where
rn1 = 1
union all
sel
rslt.id,
rslt.nm || b.nm,
b.rn1,
rslt.lvl +1 as lvl
from
tb3 b
inner join
rslt
on
rslt.id = b.id
where
rslt.rn1 < b.rn1
)
sel id,nm
from rslt
qualify rank() over (partition by id order by lvl desc ) = 1;
Regards,
R.Rajeev