简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM