简体   繁体   中英

How to identify or extract the rows where two column values (Amount fields) are different in SQL

Rather than manually going through millions of records, is there a way to find the records which has difference in two amount columns? Is there a query to do it for me?

For example, Table A contains ColA and ColB with amountss on this and the datatype is "Money" on both columns. There are few instances where values are different (It's decimal values, apparently, yeah? Yes, because its money field).

Can somebo0dy help me with a query to extract the rows with difference in money values?

As both are same datatype. You just need to check ColA <> ColB.

SELECT * FROM TableA  WHERE ColA <> ColB 

To consider rows where ColA or ColB are null you can write as:

select ColA, ColB
from TableA
where Isnull(ColA,0) - Isnull(ColB ,0) != 0

Demo

In general, you can query rows having some conditions using where clause.

Select * from Table where Column1 operator Column2

So, here you can write:

Select * from TableA where ColA <> ColB

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