简体   繁体   English

SQL SUM返回错误的结果

[英]SQL SUM Returns wrong result

Hello I am trying to get the right SUM from my query, and i know that this works: 您好,我正在尝试从查询中获取正确的SUM,并且我知道这可行:

SELECT SUM(AMOUNT) From IncomingInvoiceLine inner Join [File] ON IncomingInvoiceLine.FILENUMBER = [File].FILENUMBER WHERE [File].RELATIONCODE = '12TU01'

But this is the amount without the currency being calculated so i tried this: 但这是没有计算货币的金额,所以我尝试了这个:

    SELECT fmsTotalAmountIncoming INTO TempIncomingAmounts FROM (
        SELECT SUM(CASE WHEN fms1.currency != 'EUR'
            THEN fms1.amount * fms1.rate
        ELSE ISNULL(fms1.amount, 0) END ) fmsTotalAmountIncoming
    FROM [fms].[dbo].[IncomingInvoiceLine] fms1
    WHERE fms1.RELATIONCODE = '12TU01'
    ) a 

    SELECT fmsTotalAmountIncoming FROM [fms].[dbo].[TempIncomingAmounts]

    DROP TABLE [fms].[dbo].[TempIncomingAmounts]

And this does not return the right result, it returns NULL while the first query returns: 并且这不会返回正确的结果,它在第一个查询返回时返回NULL:

8145.46 8145.46

Yet I can't figure out why the query with the currency being converted returns NULL. 但是我无法弄清楚为什么要转换货币的查询返回NULL。 It should return 它应该返回

8106.546 8106.546

(I first made this in vb.net and then wanted to make it faster by writing a stored procedure). (我首先在vb.net中做到了这一点,然后想通过编写存储过程使其更快)。

Does anyone see why it does this? 有人知道为什么这样做吗?

I Think your multiplication may causing the problem. 我认为您的乘法可能会导致问题。 Because i have tried the same problem with some dummy data and it is giving the the accurate result means it is not giving any null value 因为我已经用一些虚拟数据尝试了相同的问题,并且给出了准确的结果,这意味着它没有给出任何空值

 SELECT fmsTotalAmountIncoming INTO TempIncomingAmounts FROM (
    SELECT SUM(CASE WHEN fms1.currency != 'EUR'
        THEN fms1.amount * 0.5
    ELSE ISNULL(fms1.amount, 0) END ) fmsTotalAmountIncoming
FROM [fms].[dbo].[IncomingInvoiceLine] fms1
WHERE fms1.RELATIONCODE = '12TU01'
) a 

SELECT fmsTotalAmountIncoming FROM [fms].[dbo].[TempIncomingAmounts]

DROP TABLE [fms].[dbo].[TempIncomingAmounts]

If the above query results the correct result then its the multipilcation problem. 如果以上查询得出正确的结果,则说明它是多重问题。

It's because our fms1.amount * fms1.rate. 这是因为我们的fms1.amount * fms1.rate。

Rate is informat X.XX, cast it to the same format as amount . Rate为X.XX格式,将其转换为与amount相同的格式。

It can be conversely. 相反。 Add more info (data type of amount and data type of rate) for more detailed answer. 添加更多信息(金额的数据类型和费率的数据类型)以获得更详细的答案。

may be one of the null value in multiplication. 可能是乘法中的空值之一。 You can figure out following way 您可以找出以下方式

ISNULL(fms1.amount * fms1.rate, 0) ISNULL(fms1.amount * fms1.rate,0)

The problem in the end was my stupidity, i was trying to get it directly from the IncomingInvoiceLine, while i needed to inner join on file. 最后的问题是我的愚蠢,我试图直接从IncomingInvoiceLine获取它,而我需要对文件进行内部联接。 Now it works, using this: 现在可以使用以下方法工作:

SELECT fmsTotalAmountIncoming INTO TempIncomingAmounts FROM (
    SELECT SUM(CASE WHEN fms1.currency != 'EUR'
        THEN fms1.amount * fms1.rate
    ELSE ISNULL(fms1.amount, 0) END) fmsTotalAmountIncoming
FROM [fms].[dbo].[file] f
    INNER JOIN [fms].[dbo].[incominginvoiceline] fms1 ON 
    fms1.filenumber = CONVERT(NVARCHAR, f.filenumber)
    WHERE f.RELATIONCODE = @RelationCode
) a 

Thank you for all the help! 感谢您的所有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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