473,406 Members | 2,371 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,406 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 3929

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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.