How can I determine if a set of rows in one table (with a specific id) represents all of the rows in another table with the same id?
I don't know how to create a query to determine whether or not I have a success case, as noted below. I am trying to avoid using a WHILE loop in TSQL, and would much rather use a set-based operation.
Fail case:
Only 2 of the records in Table2 are represented in Table1.
Table1
| Table1_id | Table2_id | special_id |
|---|---|---|
| 1 | 11 | 47 |
| 2 | 11 | 48 |
Table2
| Table2_id | special_id |
|---|---|
| 11 | 45 |
| 11 | 46 |
| 11 | 47 |
| 11 | 48 |
| 11 | 49 |
Success case:
All records in Table2 are represented in Table1.
Table1
| Table1_id | Table2_id | special_id |
|---|---|---|
| 1 | 11 | 45 |
| 2 | 11 | 46 |
| 3 | 11 | 47 |
| 4 | 11 | 48 |
| 5 | 11 | 49 |
Table2
| Table2_id | special_id |
|---|---|
| 11 | 45 |
| 11 | 46 |
| 11 | 47 |
| 11 | 48 |
| 11 | 49 |
The expected result for my Success case would be the distinct Table2.Table2_id value 11.
If you
LEFT JOINTable2 to Table1 and thenGROUP BYtable2_id, you can add aHAVINGcondition that compares the count of all Table2 rows with those having a matching Table1 row. If equal, all rows match and the table2_id can be included in the result.Something like:
If your ID list came from another source and you needed to verify that all matching Table2 rows for each ID had matching Table1 rows, you could use a
NOT EXISTS()test as follows:Note that the above will also pass if an ID has no matching Table2 rows. Since there are no Table2 rows, there are no unmatched rows, and the criteria passes. (Think of it like: What are the prerequisites for a class? None? Then, the prerequisite criteria is always satisfied and anyone can get in.)
See this db<>fiddle for a demo with some extra data.