Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My app knows a list of let's say 5 to max. 50 IDs (GUID form) and I'm searching for a single MS SQL query that a table contains rows with all of them. I don't need to know which one is maybe missing, just: We have them all or not.

I'm looking for something like
SELECT COUNT * FROM myTable WHERE ID IN (ID1, ID2, ...IDn)

and if result number equals number of IDs I have them all.
Thank you for any help!
LiTe

What I have tried:

Made my thoughts to easiest way, but I'm no SQL guru :)
Posted
Updated 6-Jul-20 10:01am

1 solution

Well, FWIW, I would do
SELECT ID, COUNT(*) FROM myTable group by ID
'possibly'
SELECT ID, COUNT(*) FROM MyTable WHERE ID In (ID1, ID2, .. IDn) GROUP BY ID


The advantage comes if you then at some stage need to check the missing ones, while you can quickly sum the count(s) if that's what you need

You could also look at the 'HAVING' clause, ie,
HAVING COUNT = 0
SQL HAVING Clause[^]
 
Share this answer
 
v2
Comments
LightTempler 7-Jul-20 17:43pm    
Thank you very much!
Exactly what I was looking for!

LiTe

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900