tsql - Select all Items which don't have a particular value in another table -


table: car columns: carid, value  table: tyres columns: tyreid, carid, brand 

how can select cars don't have corresponding tyre of brand. in scenario car have number of different tyres each of has different brand.

e.g.

car1 has 4 tyres, 2 x brand a, 1 x brand b, 1 x brand c.
car2 has 3 tyres, 1 x brand a, 2 x brand b.
car3 has 4 tyres, 3 x brand a, 1 x brand c.

i want find out carid of vehicles don't have single tyre of brand c. in instance result car2.

example data:

car carid   value -------------- 1       abc 2       def 3       geh  tyre tyreid  carid   brand ---------------- 1       1   brand 2       1   brand 3       1   brand b 4       1   brand c 5       2   brand 6       2   brand b 7       2   brand b 8       3   brand 9       3   brand 10      3   brand 11      3   brand c  result id      2 

use not exists instead not in because in clause have limit of 2500 rows

select carid  car not exists (   select 'x'   tyres   brand='brand c'   , tyres.carid = car.carid ) 

Comments