RECURSIVE function logic
createmultisetvolatiletable tb1
(id integer
,nm varchar(5)
)
primaryindex (id,nm)
oncommitpreserve rows;
insertinto tb1 values (10,'xy');
insertinto tb1 values (10,'yz');
insertinto tb1 values (10,'zx');
insertinto tb1 values (20,'ab');
insertinto tb1 values (20,'bc');
createmultisetvolatiletable tb3
as
(
sel id,nm,rank () over (partitionby id orderby nm) rn1
from
tb1
) withdataprimaryindex (id,nm) oncommitpreserve rows;
withrecursive rslt (id,nm,rn1,lvl)
as(
sel id,cast ( nm as varchar(20)),rn1,0 as lvl
from
tb3
where
rn1 = 1
unionall
sel
rslt.id,
rslt.nm || ',' ||b.nm,
b.rn1,
rslt.lvl +1 as lvl
from
tb3 b
innerjoin
rslt
on
rslt.id = b.id
where
rslt.rn1 < b.rn1
)
sel id,nm
from rslt
qualify rank() over (partitionby id orderby lvl desc ) = 1;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Above logic used for only single grouping colum.
If we use multiple grouping statements for same, it will NOT be concatenated , Please see which we’ve written logic for multiple grouping
-----------------------Logic which we've written---------------------------------
CREATEMULTISET TABLE OLAP_DEV.TB1
(
DEALER_WID DECIMAL(10,0)
, DIVN_WID DECIMAL(10,0)
, EVTLOC_WID DECIMAL(10,0)
, MONTH_WID DECIMAL(10,0)
, MM_WID DECIMAL(10,0)
,EVENT_NAME VARCHAR(2000)
)
INSERTINTO TB1
SELECT * FROM WC_EVENT_MM_A_TEST;
SELECT * FROM TB3
CREATEMULTISETTABLE TB3
AS
(
SEL DEALER_WID,DIVN_WID, EVTLOC_WID,MONTH_WID,MM_WID, EVENT_NAME, RANK () OVER (PARTITIONBY DEALER_WID,DIVN_WID, EVTLOC_WID,MONTH_WID,MM_WID ORDERBY EVENT_NAME) RN1
FROM
TB1
)
WITHDATA;
WITHRECURSIVE RSLT (DEALER_WID,DIVN_WID, EVTLOC_WID,MONTH_WID,MM_WID, EVENT_NAME,RN1,LVL)
AS(
SEL DEALER_WID,DIVN_WID, EVTLOC_WID,MONTH_WID,MM_WID,CAST ( EVENT_NAME AS VARCHAR(1000)),RN1,0 AS LVL
FROM
TB3
WHERE
RN1 = 1
UNIONALL
SEL
--RSLT.ID,
RSLT.DEALER_WID,
RSLT.DIVN_WID,
RSLT.EVTLOC_WID,
RSLT.MONTH_WID,
RSLT.MM_WID,
TRIM(RSLT.EVENT_NAME) || ',' ||TRIM(B.EVENT_NAME),
B.RN1,
RSLT.LVL +1 AS LVL
FROM
TB3 B
INNERJOIN
RSLT
ON
--RSLT.ID = B.ID
RSLT.DEALER_WID=B.DEALER_WID
AND RSLT.DIVN_WID=B.DIVN_WID
AND RSLT.EVTLOC_WID=B.EVTLOC_WID
AND RSLT.MONTH_WID=B.MONTH_WID
AND RSLT.MM_WID=B.MM_WID
WHERE
RSLT.RN1 < B.RN1
)
SEL DEALER_WID,DIVN_WID, EVTLOC_WID,MONTH_WID,MM_WID, EVENT_NAME
FROM RSLT
QUALIFY RANK() OVER (PARTITIONBY DEALER_WID,DIVN_WID, EVTLOC_WID,MONTH_WID,MM_WID ORDERBY LVL DESC ) = 1;
this logic is not working when we are using multiple grouping values.
Please help me out from this situation!
Thanks in advance
Eagerly awaiting
RECURSIVE function logic
createmultisetvolatiletable tb1
(id integer
,nm varchar(5)
)
primaryindex (id,nm)
oncommitpreserve rows;
insertinto tb1 values (10,'xy');
insertinto tb1 values (10,'yz');
insertinto tb1 values (10,'zx');
insertinto tb1 values (20,'ab');
insertinto tb1 values (20,'bc');
createmultisetvolatiletable tb3
as
(
sel id,nm,rank () over (partitionby id orderby nm) rn1
from
tb1
) withdataprimaryindex (id,nm) oncommitpreserve rows;
withrecursive rslt (id,nm,rn1,lvl)
as(
sel id,cast ( nm as varchar(20)),rn1,0 as lvl
from
tb3
where
rn1 = 1
unionall
sel
rslt.id,
rslt.nm || ',' ||b.nm,
b.rn1,
rslt.lvl +1 as lvl
from
tb3 b
innerjoin
rslt
on
rslt.id = b.id
where
rslt.rn1 < b.rn1
)
sel id,nm
from rslt
qualify rank() over (partitionby id orderby lvl desc ) = 1;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Above logic used for only single grouping colum.
If we use multiple grouping statements for same, it will NOT be concatenated , Please see which we’ve written logic for multiple grouping
-----------------------Logic which we've written---------------------------------
CREATEMULTISET TABLE OLAP_DEV.TB1
(
DEALER_WID DECIMAL(10,0)
, DIVN_WID DECIMAL(10,0)
, EVTLOC_WID DECIMAL(10,0)
, MONTH_WID DECIMAL(10,0)
, MM_WID DECIMAL(10,0)
,EVENT_NAME VARCHAR(2000)
)
INSERTINTO TB1
SELECT * FROM WC_EVENT_MM_A_TEST;
SELECT * FROM TB3
CREATEMULTISETTABLE TB3
AS
(
SEL DEALER_WID,DIVN_WID, EVTLOC_WID,MONTH_WID,MM_WID, EVENT_NAME, RANK () OVER (PARTITIONBY DEALER_WID,DIVN_WID, EVTLOC_WID,MONTH_WID,MM_WID ORDERBY EVENT_NAME) RN1
FROM
TB1
)
WITHDATA;
WITHRECURSIVE RSLT (DEALER_WID,DIVN_WID, EVTLOC_WID,MONTH_WID,MM_WID, EVENT_NAME,RN1,LVL)
AS(
SEL DEALER_WID,DIVN_WID, EVTLOC_WID,MONTH_WID,MM_WID,CAST ( EVENT_NAME AS VARCHAR(1000)),RN1,0 AS LVL
FROM
TB3
WHERE
RN1 = 1
UNIONALL
SEL
--RSLT.ID,
RSLT.DEALER_WID,
RSLT.DIVN_WID,
RSLT.EVTLOC_WID,
RSLT.MONTH_WID,
RSLT.MM_WID,
TRIM(RSLT.EVENT_NAME) || ',' ||TRIM(B.EVENT_NAME),
B.RN1,
RSLT.LVL +1 AS LVL
FROM
TB3 B
INNERJOIN
RSLT
ON
--RSLT.ID = B.ID
RSLT.DEALER_WID=B.DEALER_WID
AND RSLT.DIVN_WID=B.DIVN_WID
AND RSLT.EVTLOC_WID=B.EVTLOC_WID
AND RSLT.MONTH_WID=B.MONTH_WID
AND RSLT.MM_WID=B.MM_WID
WHERE
RSLT.RN1 < B.RN1
)
SEL DEALER_WID,DIVN_WID, EVTLOC_WID,MONTH_WID,MM_WID, EVENT_NAME
FROM RSLT
QUALIFY RANK() OVER (PARTITIONBY DEALER_WID,DIVN_WID, EVTLOC_WID,MONTH_WID,MM_WID ORDERBY LVL DESC ) = 1;
this logic is not working when we are using multiple grouping values.
Please help me out from this situation!
Thanks in advance
Eagerly awaiting