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 – Exclude rows from SELECT statement if a certain column combination exists in that row

I have an SQL select statement with many columns. However I want to exclude an entire row if certain column combinations exist.

My query looks something like this:

SELECT COLUMN1, COLUMN2, COLUMN3
FROM TABLE
WHERE NOT (COLUMN2 = 'A' AND COLUMN3 = 'B')
AND NOT (COLUMN2= 'B' AND COLUMN3 = 'C')
.
.
.

This doesn’t seem to work, I am getting a "missing expression" error. What is the correct way to exclude entire rows when column combinations match certain conditions?

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

Thank you!

>Solution :

Since a row can only match one of those conditions at one time, you should be using OR:

SELECT COLUMN1, COLUMN2, COLUMN3
FROM YourTable
WHERE NOT (    
    ( COLUMN2 = 'A' AND COLUMN3 = 'B' )
     OR 
    ( COLUMN2= 'B' AND COLUMN3 = 'C' )
)

db<>fiddle here

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