Link to home
Create AccountLog in
Avatar of Dan Violet Sagmiller (He/Him)
Dan Violet Sagmiller (He/Him)Flag for United States of America

asked on

MS SQL, 2 Identical numbers are returned when query checks for non-matching

In the following query, I can visually inspect the result, and it returns items where the cost and amount are identical.  

SELECT r.ReceiptId, s.Name AS SourceName, SUM(rb.Amount) AS Amount, r.Cost AS Cost
FROM Receipts r
  INNER JOIN ReceiptBudgets rb ON r.ReceiptId = rb.ReceiptId
  INNER JOIN Sources s ON r.SourceId = s.SourceId
WHERE r.Cost != Amount
GROUP BY r.ReceiptId, s.Name, r.Cost

Particularly note this part -- WHERE r.Cost != Amount

Yet I get values returned where r.Cost = Amount.  

What is wrong with my query?  

PLEASE NOTE:  I have also tried this --WHERE CAST(r.Cost AS MONEY) != CAST(Amount AS MONEY)

Avatar of Nathan Riley
Nathan Riley
Flag of United States of America image

Link to home
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
>>What is wrong with my query?  <<
I suspect you are using Real or Float data types for Cost  and/or Amount.
Avatar of Dan Violet Sagmiller (He/Him)


It was quite functional, worked perfectly, but wasn't explained for the difference, or why.
I tried.  Oh well...