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

SQL select rows that have one specific value but not another in the same table

I have a table like this:

DOC DATE KEY HOUR DEPARTAMENT STATUS
99KN001000002 2000-12-28 12:04:51 DEP 1
99KN001000002 2000-12-28 12:05:35 DEP 2
99KN001000002 2000-12-28 12:06:31 DEP 3
99KN001000002 2000-12-28 12:07:17 DEP 4
99KN001000003 2000-12-29 12:07:17 DEP 1

and I would like to find out all those documents that have "DEP 1" as the only record

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 :

This should work in any RDBMS:

select DOC from table_name
where DEPARTAMENT = 'DEP 1'
and DOC not in
(select DOC from table_name where DEPARTAMENT <> 'DEP 1');

You can use MINUS or EXCEPT if your RDBMS supports those.

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