473,218 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

Recursive CTE Permutations Help

3 2Bits
I have the following example, which is reaching the max recursion.

Declare @example table (RowID varchar(20), RelID varchar(20))
Insert Into @example Select 'Rec1', 'Rec5'
Insert Into @example Select 'Rec1', 'Rec6'
Insert Into @example Select 'Rec5', 'Rec1'
Insert Into @example Select 'Rec5', 'Rec2'
Insert Into @example Select 'Rec6', 'Rec1'
Insert Into @example Select 'Rec6', 'Rec3'
Insert Into @example Select 'Rec6', 'Rec4'
Insert Into @example Select 'Rec6', 'Rec5'

;With RelatedCTE (RowID, RelID, ComboID) as
(
Select RowID, RelID, RowID + '-' + RelID
From @example
UNION ALL
Select r1.RowID, r2.RelID, r1.RowID + '-' + r2.RelID
From RelatedCTE r1
INNER JOIN @example r2 ON r1.RelID = r2.RowID
Where r1.ComboID <> r1.RowID + '-' + r2.RelID
and r1.RowID <> r2.RowID
--and r2.RelID <> r1.RelID
)
Select *
From RelatedCTE


What I'm trying to get out of the output, is a unique list that looks like this:
Rec1-Rec5
Rec1-Rec6
Rec5-Rec1
Rec5-Rec2
Rec6-Rec1
Rec6-Rec3
Rec6-Rec4
Rec6-Rec5

Rec1-Rec2
Rec1-Rec3
Rec1-Rec4
Rec5-Rec3
Rec5-Rec4
Rec5-Rec6
Rec6-Rec2
Rec2-Rec1
Rec2-Rec3
Rec2-Rec4
Rec2-Rec5
Rec2-Rec6
Rec3-Rec1
Rec3-Rec2
Rec3-Rec4
Rec3-Rec5
Rec3-Rec6
Rec4-Rec1
Rec4-Rec2
Rec4-Rec3
Rec4-Rec5
Rec4-Rec6
Jul 20 '21 #1
0 3799

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: SimonVC | last post by:
Is there a way to do this as a list comprehension? >>> def recu(alist, blist=): .... if len(alist)==0: print blist .... for i in range(len(alist)): .... ...
10
by: Steve Goldman | last post by:
Hi, I am trying to come up with a way to develop all n-length permutations of a given list of values. The short function below seems to work, but I can't help thinking there's a better way. ...
9
by: JP SIngh | last post by:
Hi All I am trying to write a recursive function to list the managers and his employees in a tree like sctructure Manager 1 Emp1 Emp1.1 Emp 1.2 Emp 2
2
by: poofpoof | last post by:
Dear Experts, I have some problems with XSLT recursivity... here what I want to do XML part : ---------- <struct>
3
by: | last post by:
Hello All, this is my first post. OK - The goal is to display the following (note: substitute 1' ' for 2'*') by using 3 recursive functions. 0123454321001234543210 **012343210012343210**...
2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
0
by: thygremlin | last post by:
Hi everyone, I have come by a challenge.... I have the following XML file, which represents expressions with "+" and "*" artihmentic operands: <m value="10"> <i value="5"/> <m...
3
by: NatRoger | last post by:
Hi, We are using DB2 V7 (MVS) in our shop, which dont have the luxury of the 'WITH clause' - CTE support to accomplish recursive queries. Here is my problem - Table1 From-ID To-ID A1 ...
1
Rabbit
by: Rabbit | last post by:
Introduction Starting with SQL Server 2005, you have the ability to create recursive queries using common table expressions (CTE). They are very powerful tools that can be used to query hierarchical...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.