Max_ID Second_Max_ID Cumulative_ID
173 97 ,97
174 173 ,97,173
... ... ...
I need to write a query to compute the cumulative id. Can someone help? It's a temporary table with 3 columns(Max_ID,Second_Max_ID,Cumulative_ID) where I have just Max_ID and Second_Max_ID value in table and I need to compute Cumulative_ID for each row in the same temporary table.
I used query provided by shnugo and modified the query as below -
DECLARE @mockup TABLE(Max_ID INT,Second_Max_ID INT);
INSERT INTO @mockup VALUES
(173 , 97 )
,(174 , 173 );
WITH recCTE AS
(
SELECT Max_ID
,Second_Max_ID
,CAST(Second_Max_ID AS VARCHAR(MAX)) AS Cumulative_ID
FROM @mockup --WHERE Second_Max_ID IS NULL
UNION ALL
SELECT m.Max_ID
,m.Second_Max_ID
,r.Cumulative_ID+','+ cast(m.Second_Max_ID as varchar(max))
FROM @mockup m
INNER JOIN recCTE r ON r.Max_ID=m.Second_Max_ID
)
SELECT * FROM recCTE;
Now getting result like -
Max_ID Second_Max_ID Cumulative_ID
173 97 97
174 173 173
174 173 97,173
How can I remove the second row from above result?