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 filter if ID doesn't have a value in another column

I have this sample table

Payment ID Type
123 Fee
123 Service
123 Finance
456 Fee
456 Service

I’m trying to achieve a table that would filter out any row where an ID doesn’t have "type" "Finance".

Expected result would be

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

Payment ID Type
456 Fee
456 Service

>Solution :

A readable alternative is:

with
filter_table as (
select payment_id
from your_table
where type = 'Finance'
)    
select *
from your_table
where payment_id is not in (select id from filter_table)

An alternative without the sub-query could be:

select *
from your_table
where payment_id is not in (select id from your_table where type='Finance')
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