如何查询实际金额的1.00以内的金额列?
例如,如果AmountPaid = 7.75,我想返回所有结果,其中金额在6.75 - 8.75之间。
我知道我忽略了一些简单的事情,但截至目前,我有以下代码......
Where (vc.AmountPaid >= med.Payment - 1.00 or vc.amountpaid <= med.Payment + 1.00)
答案 0 :(得分:1)
Where (vc.AmountPaid between med.Payment - 1.00 and med.Payment + 1.00)
答案 1 :(得分:0)
您应该用AND替换OR关键字。
类似这样的事情
Where (vc.AmountPaid >= med.Payment - 1.00 AND vc.amountpaid <= med.Payment + 1.00)
答案 2 :(得分:0)
您正在忽略ABS功能。
获得所需内容的最简单方法是:
where abs(AmountPaid-payment) <= 1
当我处理数字差异时,ABS是好的。 10和12之间的差异,12和10是相同的:2。注意这个例子(你可以只复制/粘贴到SSMS并运行):
declare @yourdata table (AmountPaid money, Payment money);
insert @yourdata values (50.00, 49.12),(100, 100.91),(25,1000),(10,20);
select AmountPaid, Payment, diff = abs(AmountPaid-payment)
from @yourdata
where abs(AmountPaid-payment) <= 1;
<强>返回强>
AmountPaid Payment diff
--------------------- --------------------- ---------------------
50.00 49.12 0.88
100.00 100.91 0.91