0

I have T1 table with Id and name columns.

Table T2 with Aggregate_ID and Element_ID.

They are crossed

x1:

ID и Name

1 Car
2 Hood
3 Engine
4 Cylinder 
5 Wheel
6 tyre
7 rim (car)
8 Rim fixation (Car)

x2:

Aggregate_ID Element_ID

1 2
1 3 
1 4
1 5 
3 4
5 6
5 7
7 8 

I need to select simplest element like 2, 4, 8

Complexity and number of elements can be varied.

How can I do it with recursion?

There is another task:

I need to output all the simplest elements of which consists Wheel.

streamc
  • 676
  • 3
  • 11
  • 27

1 Answers1

2

Recursive solution in SQL can be very complex. In your case I see no need to use it, since it will only make your code more complex.

You can use CTE if you still insist: Recursive query in SQL Server

Non- recursive solution:,
You want only the elements that appear in T2 in the Element_ID but not in the Aggregate_ID:

SELECT Element_ID
FROM T2
EXCEPT
SELECT Aggregate_ID 
FROM T2

Or if you want to display all of the information for the elements:

SELECT *
FROM T1
WHERE T1.ID NOT IN (SELECT Aggregate_ID 
                    FROM T2)
Community
  • 1
  • 1
Yosi Dahari
  • 6,794
  • 5
  • 24
  • 44