goofyui
goofyui

Reputation: 3492

How to compare records in a SQL Table

I have a table name TransactionTable. If InsuranceCode = 'ADAP' and IsADAP = 1, I have to filter these records and return the remaining records.

I am trying to write something similar to this syntax.

select *  from TransactionTable
where  Not Exists
(select * from TransactionTable where InsuranceCode = 'ADAP' and IsADAP = 1 )

Total records in TransactionTable is 1832 and Total records on filtration criteria is 109. I expect 1723 records as output. Below syntax returns only 785 records. I am aware that below syntax is incorrect.

select * from TransactionTable 
where InsuranceCode <> 'ADAP' and IsADAP <> 1

How to compare a table records and get the output.

Upvotes: 1

Views: 64

Answers (1)

RBarryYoung
RBarryYoung

Reputation: 56725

This is probably what you want:

select * from TransactionTable 
where (InsuranceCode <> 'ADAP' or IsADAP <> 1)

Lookup DeMorgan's laws if you want to understand why.

Note that the above has the same meaning as this version: (disregarding NULLs)

select * from TransactionTable 
where NOT (InsuranceCode = 'ADAP' and IsADAP = 1)

.. which is because of DeMorgan's laws.

Upvotes: 3

Related Questions