## Recursive SQL

 Author Message syedathariqbal SSCommitted Group: General Forum Members Points: 1583 Visits: 623 Hi All,Need help on some recursive Data. I would need to get 4 records out of below available If I pass value 395. Since, for 395, COlD (Child of ColA) has value 394. Again we check ColaA for 394 and find the corresponding value in ColD which is 347. Again in ColA 347 corresponds to 0 value in ColD. Total of 4 records.`ColA ColB ColC ColD ColE ColF347 AA 25 0 0 0362 BB 25 347 0 50363 CC 25 362 0 51364 DD 25 347 0 50365 EE 25 364 0 51367 FF 25 365 0 54369 GG 25 347 0 50373 HH 25 347 0 50374 II 25 373 0 51375 JJ 25 374 0 54376 KK 25 374 0 54394 LL 25 347 0 50395 MM 25 394 0 51`I beleive Using While loop this can be done. Any suggestions ? ChrisM@Work SSC Guru Group: General Forum Members Points: 97651 Visits: 20709 syedathariqbal (5/10/2013)Hi All,Need help on some recursive Data. I would need to get 4 records out of below available If I pass value 395. Since, for 395, COlD (Child of ColA) has value 394. Again we check ColaA for 394 and find the corresponding value in ColD which is 347. Again in ColA 347 corresponds to 0 value in ColD. Total of 4 records.`ColA ColB ColC ColD ColE ColF347 AA 25 0 0 0362 BB 25 347 0 50363 CC 25 362 0 51364 DD 25 347 0 50365 EE 25 364 0 51367 FF 25 365 0 54369 GG 25 347 0 50373 HH 25 347 0 50374 II 25 373 0 51375 JJ 25 374 0 54376 KK 25 374 0 54394 LL 25 347 0 50395 MM 25 394 0 51`I beleive Using While loop this can be done. Any suggestions ?Sure: set this table up as a CREATE TABLE & INSERTs to populate it, test that the script executes without errors, then post it here. “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor fast, accurate and documented assistance in answering your questions, please read this article.Understanding and using APPLY, (I) and (II) Paul White Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff ModenExploring Recursive CTEs by Example Dwain Camps syedathariqbal SSCommitted Group: General Forum Members Points: 1583 Visits: 623 Here it is....`CREATE TABLE [dbo].[tmpTable]( [ColA] [nchar](10) NOT NULL, [ColB] [nchar](10) NULL, [ColC] [nchar](10) NULL, [ColD] [nchar](10) NULL, [ColE] [nchar](10) NULL, [ColF] [nchar](10) NULL, CONSTRAINT [PK_tmpTable] PRIMARY KEY CLUSTERED ( [ColA] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOINSERT [dbo].[tmpTable] ([ColA], [ColB], [ColC], [ColD], [ColE], [ColF]) VALUES (N'347 ', N'A ', N'25 ', N'0 ', N'0 ', N'0 ')INSERT [dbo].[tmpTable] ([ColA], [ColB], [ColC], [ColD], [ColE], [ColF]) VALUES (N'362 ', N'B ', N'25 ', N'347 ', N'0 ', N'50 ')INSERT [dbo].[tmpTable] ([ColA], [ColB], [ColC], [ColD], [ColE], [ColF]) VALUES (N'363 ', N'C ', N'25 ', N'362 ', N'0 ', N'51 ')INSERT [dbo].[tmpTable] ([ColA], [ColB], [ColC], [ColD], [ColE], [ColF]) VALUES (N'364 ', N'D ', N'25 ', N'347 ', N'0 ', N'50 ')INSERT [dbo].[tmpTable] ([ColA], [ColB], [ColC], [ColD], [ColE], [ColF]) VALUES (N'365 ', N'E ', N'25 ', N'364 ', N'0 ', N'51 ')INSERT [dbo].[tmpTable] ([ColA], [ColB], [ColC], [ColD], [ColE], [ColF]) VALUES (N'367 ', N'F ', N'25 ', N'365 ', N'0 ', N'54 ')INSERT [dbo].[tmpTable] ([ColA], [ColB], [ColC], [ColD], [ColE], [ColF]) VALUES (N'369 ', N'G ', N'25 ', N'347 ', N'0 ', N'50 ')INSERT [dbo].[tmpTable] ([ColA], [ColB], [ColC], [ColD], [ColE], [ColF]) VALUES (N'373 ', N'H ', N'25 ', N'347 ', N'0 ', N'50 ')INSERT [dbo].[tmpTable] ([ColA], [ColB], [ColC], [ColD], [ColE], [ColF]) VALUES (N'374 ', N'I ', N'25 ', N'373 ', N'0 ', N'51 ')INSERT [dbo].[tmpTable] ([ColA], [ColB], [ColC], [ColD], [ColE], [ColF]) VALUES (N'375 ', N'J ', N'25 ', N'374 ', N'0 ', N'54 ')INSERT [dbo].[tmpTable] ([ColA], [ColB], [ColC], [ColD], [ColE], [ColF]) VALUES (N'376 ', N'K ', N'25 ', N'374 ', N'0 ', N'50 ')INSERT [dbo].[tmpTable] ([ColA], [ColB], [ColC], [ColD], [ColE], [ColF]) VALUES (N'394 ', N'L ', N'25 ', N'347 ', N'0 ', N'50 ')INSERT [dbo].[tmpTable] ([ColA], [ColB], [ColC], [ColD], [ColE], [ColF]) VALUES (N'395 ', N'M ', N'25 ', N'394 ', N'0 ', N'51 ')` Mark Cowne SSChampion Group: General Forum Members Points: 14844 Visits: 26578 I get 3 rows not 4, maybe I'm missing something.`DECLARE @Start INT = 395;WITH Recur AS ( SELECT [ColA], [ColB], [ColC], [ColD], [ColE], [ColF] FROM [dbo].[tmpTable] WHERE [ColA] = @Start UNION ALL SELECT t.[ColA], t.[ColB], t.[ColC], t.[ColD], t.[ColE], t.[ColF] FROM [dbo].[tmpTable] t INNER JOIN Recur r ON r.[ColD] = t.[ColA])SELECT [ColA], [ColB], [ColC], [ColD], [ColE], [ColF]FROM Recur;` ____________________________________________________Deja View - The strange feeling that somewhere, sometime you've optimised this query beforeHow to get the best help on a forumhttp://www.sqlservercentral.com/articles/Best+Practices/61537 ChrisM@Work SSC Guru Group: General Forum Members Points: 97651 Visits: 20709 Thanks...`WITH Resolver AS ( SELECT [Level] = 1, [Route] = CAST(RTRIM(t.ColA) AS VARCHAR(25)), t.ColA, t.ColB, t.ColC, t.ColD, t.ColE, t.ColF FROM tmpTable t WHERE ColA = 395 UNION ALL SELECT [Level] = r.[Level] + 1, [Route] = CAST(r.[Route] + '>' + CAST(RTRIM(t.ColA) AS VARCHAR(25)) AS VARCHAR(25)), t.ColA, t.ColB, t.ColC, t.ColD, t.ColE, t.ColF FROM Resolver r INNER JOIN tmpTable t ON t.ColA = r.ColD)SELECT * FROM Resolver ORDER BY [Level], ColA;-- three rows are returned: There is no row with value=0 in ColA.` “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor fast, accurate and documented assistance in answering your questions, please read this article.Understanding and using APPLY, (I) and (II) Paul White Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff ModenExploring Recursive CTEs by Example Dwain Camps ChrisM@Work SSC Guru Group: General Forum Members Points: 97651 Visits: 20709 Quick work, Mark ;-) “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor fast, accurate and documented assistance in answering your questions, please read this article.Understanding and using APPLY, (I) and (II) Paul White Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff ModenExploring Recursive CTEs by Example Dwain Camps syedathariqbal SSCommitted Group: General Forum Members Points: 1583 Visits: 623 Thanks Chris and Mark. Exactly what I needed. Jeff Moden SSC Guru Group: General Forum Members Points: 511765 Visits: 44303 syedathariqbal (5/10/2013)Thanks Chris and Mark. Exactly what I needed.The next question would be.... do you know how it works? --Jeff ModenRBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair Helpful Links:How to post code problemsHow to post performance problemsForum FAQs