SQL Server Check if a group contains all items from another table

rdlzhqv9  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(109)

I want to check if Sequence is complete at Sequence index.

I have a table @workREQUIREMENTS like this that has the required/planned INTRAWORKNO for a sequence:
| INTRAWORKNO |
| ------------ |
| 10 |
| 20 |
| 30 |

and I have a table @results like this:

SEQUENCEINTRAWORKNOpart
1100
1300
2100
2200
2300
3100
3101
3102
6400
7400
8100
8200
8201
8300

How can I delete this SEQUENCEs, where I did not have ALL INTRAWORKNO in the corresponding SEQUENCE ?

As you see,

  • sequence 1 has no INTRAWORKNO 20, which eleminates this sequence --> NG
  • sequence 2 has ALL INTRAWORKNO --> OK
  • sequence 3 has no INTRAWORKNO 20 and no 30 --> NG
  • sequence 6 and 7 have no values from the @workREQUIREMENTS -table --> NG
  • sequence 8 has ALL INTRAWORKNO (Intraworkno 20 is divided in sub-parts) --> OK

A group by clause with a count brings not the expected results, because there can be multiple sub-parts (characteristics) of these SEQUENCE

The desired results are in bold.

js4nwp54

js4nwp541#

The looks like relational division. You can use double not exists for this ( explanation here ):

select *
from results as r1
where not exists (
    select *
    from requirements as reqd
    where not exists (
        select *
        from results as r2
        where r1.sequence = r2.sequence and r2.intraworkno = reqd.intraworkno
    )
);

DB<>Fiddle

相关问题