ROUND()不允许按预期加入

时间:2018-07-26 23:44:36

标签: sql sql-server

我有一个SQL查询(使用SQL Server),用于删除一个表中存在于另一表中的所有行。问题在于,Amount列之一是十进制值,而在一个表中,这些值是四舍五入的,而在另一表中则不是。我认为通过使用ROUND函数,可以将两者视为相同,但事实并非如此。我正在使用ROUND(value, 2),这是示例列,我希望四舍五入后的值相等。

+----------------------+----------------------+
| Amount Column TableA | Amount Column TableB |
+----------------------+----------------------+
|        1703.98       |       1703.975       |
+----------------------+----------------------+


DELETE t1 
FROM [dbo].[TableA] t1
JOIN [dbo].[TableB] t2 ON ISNULL(t1.[Account], '') = ISNULL(t2.[Account], '') 
                       AND ISNULL(t1.[Date], '') = ISNULL(t2.[Date], '') 
                       AND ISNULL(ROUND(t1.[Amount], 2, 0), '') = ISNULL(ROUND(t2.[Amount], 2, 0), '')

我想念什么?

2 个答案:

答案 0 :(得分:1)

您不能舍入整数吗?

 DELETE TableA
 FROM TableA T1
 LEFT OUTER JOIN TableB T2
 ON T1.Account = T2.Account
    AND T1.[Date] = T2.[Date]

 WHERE 
 ROUND(T1.Amount,0) = ROUND(T2.Amount,0)

或者,如果不是,则将两个数字相同:

    DELETE #TableA
    FROM #TableA T1
    JOIN #TableB T2
    ON T1.Account = T2.Account
      AND T1.[Date] = T2.[Date]
      AND ROUND(CAST(T1.Amount AS decimal(9,2)),2,0) = ROUND(CAST(T2.Amount AS decimal(9,2)),2,0)

答案 1 :(得分:0)

为什么不摆脱那些ISNULL呢?
这样您就可以将数字与数字进行比较,将日期与日期进行比较。

t1.Amount的数量已经是2位小数,因此不需要将其舍入为2位吗?

DELETE t1 
FROM [dbo].[TableA] t1
JOIN [dbo].[TableB] t2 
  ON t1.[Account] = t2.[Account] AND
     CAST(t1.[Date] AS DATE) = CAST(t2.[Date] AS DATE) AND
     t1.[Amount] = ROUND(t2.[Amount], 2)