Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Negative with multiple conditions

Tags:

sql

Im having a doubt in SQL statement where i have negation condition using 2 fields using the sample below:

COLUMN A | COLUMN B
  TRA    |   ORIG
  TRA    |   ORIG
  TERM   |    _N
  TRA    |   ORIG
ORIG-REV1|    _N
ORIG-REV2|    _N

im trying get filter the conditions where

"COLUMN B"='_N' and "COLUMN  A" in ('ORIG-REV1','ORIG-REV2'). 

Currently im using the condition

"COLUMN B" <>'_N' and ("COLUMN  A" <> 'ORIG-REV1' or "COLUMN  A" <> 'ORIG-REV2')

but this condition is filtering me the record where "COLUMN A"=TERM because COLUMN_B value is '_N'

the OUTPUT i needed is:

COLUMN A | COLUMN B
  TRA    |   ORIG
  TRA    |   ORIG
  TERM   |    _N
  TRA    |   ORIG

i know in postgres i just need to use Not("COLUMN B"='_N' and "COLUMN A" in ('ORIG-REV1','ORIG-REV2')), unfortunately the technology im using is very old and rudimentary and not doesn't even exist (only =, <>, >, <, OR, IN, LIKE). is there a way to get this filter in another way?

Best Regards, Paulo Serra

like image 448
macieira Avatar asked Jul 16 '26 23:07

macieira


1 Answers

Sorry, deleted my comment thinking I'd made a logical flub. All good.

Fiddle: https://dbfiddle.uk/-hbQem22

It's SQL Server, but hopefully that tool your are using isn't so limited that the spirit of this won't work.

Just for reference, https://en.wikipedia.org/wiki/De_Morgan%27s_laws

create table some_test_data
  (
    column_a varchar(10),
    column_b varchar(10)
  )

insert into some_test_data values ('TRA','ORIG');
insert into some_test_data values ('TRA','ORIG');
insert into some_test_data values ('TERM','_N');
insert into some_test_data values ('TRA','ORIG');
insert into some_test_data values ('ORIG-REV1','_N');
insert into some_test_data values ('ORIG-REV2','_N');

select * 
  from some_test_data
 where column_b <> '_N'
    or (
       column_a <> 'ORIG-REV1'
   and column_a <> 'ORIG-REV2'
       )
like image 153
Error_2646 Avatar answered Jul 20 '26 08:07

Error_2646



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!