Reputation: 3541
I have an Invoice which has LineItems and each LineItem has a Part_No which is from Product.
How can I write Sql to find invoices that have 2 different Part_No; i.e. Part_No 1234 and 2345?
I'm not sure how to even begin this one...
Sql for MS Sql Server is preferred but not a must.
Thanks
Upvotes: 0
Views: 27
Reputation: 1269873
I approach these problems using group by
and having
:
select invoiceid
from lineitems li
where part_no in (1234, 2345)
group by invoiceid
having count(distinct part_no) = 2;
Upvotes: 1