Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select records based on count of other records

I have a data table like

ID category
1  1
2  1
3  2
4  2
5  4

I wish to select all records that fall into two given categories i and j (for instance i=1 and j=4 would return records 1, 2 and 5). But here’s the twist: if one of the of the categories has no records, the query should return an empty set. For instance if I query for i=1 and j=3.
Can this be done, solely using SQL?

I’ve been trying to make an intermediate count table, but I’m afraid my sql isn’t up to par.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just write down what you said, will give:

SELECT *
FROM table1
WHERE (SELECT count(*) FROM table1 WHERE category=1)>0 
  AND (SELECT count(*) FROM table1 WHERE category=4)>0 

see: DBFIDDLE

or, slightly different:

SELECT *
FROM table1
WHERE (SELECT count(distinct category) FROM table1 WHERE category IN (1,4))=2

P.S. I choose to use mysql in the DbFiddle, but this should work in other DBMS’s too.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading