Recursive SQL

Posted on 2011-09-21
Last Modified: 2012-05-12

I Have a table like below

ProductID          RepProdID
######           ###########
a                         b
a                         c
a                         d
b                         a
b                         e
b                         f
b                         z
c                         a
c                         k

Means, The product a is replaced by b and etc

Now, If I query with product ID "a" then I want the output

B, C, D, E, F, Z, K.

Since product "a" is the source of all this products (please note that b to a is also possible which leads to infinite loop).

I want t-sql query to achieve this without infinite recursive loop.

How can I achieve this? Please help.
Question by:radcaesar
LVL 42

Accepted Solution

dqmq earned 500 total points
ID: 36574199
Using a view, substitute your view and table names for T and V

create view V as
select productid, repprodid from T
   where ProductID < RepProdID
      or not exists (select * from T as C where T.productID = c.Repprodid and c.productid = t.repprodid)
group by productid, repprodid

;with cte
select T.RepProdID from V as T where T.ProductID = 'f'
union all
select T.RepProdID from V as T
inner join cte on cte.RepProdID = T.ProductID
select * from cte  
LVL 21

Expert Comment

ID: 36574282
Typically, infinite loops in a hierarchy means that data has errors, but here is another way to do it.  It actually looks for repeating patterns and then ignores them.



SET @Anchor = 'a'

;WITH RecursiveCTE
	SELECT ProductID, RepProdID, 1 AS [Level], 'N' AS Cycle,
		CAST('.' + CAST(ProductID AS VARCHAR(8)) + '.' AS VARCHAR(MAX)) AS CTEPath
	FROM YourTable
	WHERE ProductID = @Anchor
	SELECT a.ProductID, a.RepProdID, b.[Level] + 1 AS [Level],
			WHEN b.CTEPath LIKE '%.' + CAST(a.ProductID AS VARCHAR(8)) + '.%' THEN 'Y'
			ELSE 'N'
		CAST(b.CTEPath + CAST(a.ProductID AS VARCHAR(8)) + '.' AS VARCHAR(MAX))
		RecursiveCTE b ON a.ProductID = b.RepProdID AND b.Cycle = 'N'
	RepProdID <> @Anchor
OPTION(Maxrecursion 100)

Open in new window


Author Comment

ID: 36574339
Just a clarification

It will work for values like ML005J, 5XP00M right.

I have this doubt because of the below where condition check.

where ProductID < RepProdID

Please explain this.
LVL 42

Expert Comment

ID: 36574869
>It will work for values like ML005J, 5XP00M right.

Well, I think so...< works fine for character values.

The goal is to eliminate the bi-directional parallelism (a replaces b, b replaces a) and infinite recursions (a replaces b, b replaces c, c replaces a).  

BTW, if "a replaces b" is equivalent to "b replaces a", then it may behoove you to add a check constraint for ProductID < RepProdID and eliminate the opposite, but equivalent relationships from your table. That makes clean retrival so much easier.  

If "a replaces b" is NOT equivalent to "b replaces a", then it's not clear what the difference might be.

 "a replaces b" is equivalent to "b replaces a"

Also, I think this view would also work and might be more self-documenting:

create view V as
select productid, repprodid from T where ProductID < RepProdID
select repprodid, productid from T where ProductID > RepProdID

LVL 29

Expert Comment

by:Olaf Doschke
ID: 36580434
The major ingredient will surely be CTE, so if you're not familiar with common table expressions used for recursion, maybe have a read on

The main question really is, whether the hierarchical data pointing back to a root node really is valid. I think so, because of my interpretation of what replacement of a product means. I may be wrong on the details, but if you think about the development of a product in the normal case older products are  replaced by newer ones only, but there might be cases a new product turns out to be less good than it's predecessor, which I imagine could be the reason a replacement product can be replaced by it's predecessor.

In that case, I'd say you could also add A to the result, as A indirectly is/was it's own replacement product. That aside the easiest yet not most effective way is to use tha maxrecursion option to cut off infinite loops after a recursion level never reached in a normal case, which is the brute force method to avoid infinite recursions. I like both the idea of checking an anchor, but the infinite loop might not go back to the anchor. Only allowing ProductID < RepProdID would not put A into the result, which you also don't want, so that would also be ok, as that only looks for forward replacements, but then there might be a history of replacements as in a->f->b->z, which would miss b and z in that case, but surely it's avoiding recursion.

It might be easier to understand and maintain to not put the recursion into the query, but instead make a query determining only the direct replacement products and use that simpler query in a recursive function starting with a root product, collecting it's direct replacement products and in the next recursion level the replacement products for those products found etc., collecting the intermediate products into a central result list, unless a call yields no further products into that list. In each level of recursion you only need to let that recursive function call itself with products neww to the list, so recursion ends naturally.

Bye, Olaf.

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question