简体   繁体   English

SQL Server中的SUM返回错误值

[英]SUM in SQL Server returning wrong values

I'm having some trouble with one specific query in SQL Server 2014. 我在使用SQL Server 2014中的一个特定查询时遇到了一些麻烦。

The sum function returns wrong result. sum函数返回错误的结果。

In my case, my SQL query return only one row, with two fields for example: cost and key. 就我而言,我的SQL查询仅返回一行,其中包含两个字段,例如:cost和key。

cost with value 10 and key with value ke890wkw. 成本为10,密钥为ke890wkw。

I don't know why, but if I run something like 我不知道为什么,但是如果我运行类似

SELECT SUM(COST) TOTAL_COST, RTRIM(KEY) 
FROM TABLE 
WHERE KEY = 'ke890wkw'

it returns 20. 返回20

The full and real query with this "bug" (?) its on picture sql2 带有此“错误”(?)的完整而真实的查询,其在图片sql2上

SELECT 
    SUM(CONVERT(FLOAT, DADOSREQF.compValorFrete)) vlr_total_frete,
    --CONVERT(FLOAT, DADOSREQF.compValorFrete) vlr_total_frete,
    (RTRIM(DADOSFORMF.NUMSOLICITACAO) + RTRIM(DADOSREQF.compCodFornecedor) + 
          RTRIM(DADOSREQF.compCodCondicaoPagamento) +  RTRIM(DADOSREQF.compFrete) + 
          RTRIM(DADOSREQF.compCodTransportadora)) chave
FROM
    FLUIG.DBO.ML001102 DADOSFORMF WITH(NOLOCK)
INNER JOIN 
    FLUIG.DBO.ML001105 DADOSREQF WITH(NOLOCK) ON DADOSREQF.DOCUMENTID = DADOSFORMF.DOCUMENTID 
WHERE
    (RTRIM(DADOSFORMF.NUMSOLICITACAO) + RTRIM(DADOSREQF.compCodFornecedor) + 
        RTRIM(DADOSREQF.compCodCondicaoPagamento) +  RTRIM(DADOSREQF.compFrete) + 
        RTRIM(DADOSREQF.compCodTransportadora)) = '1071002999439000103030F040' 
    AND DADOSFORMF.version = (SELECT MAX(version) 
                              FROM FLUIG.DBO.ML001105 vr 
                              WHERE vr.documentid = DADOSFORMF.documentid)
GROUP BY
    RTRIM(DADOSFORMF.NUMSOLICITACAO),
    RTRIM(DADOSREQF.compCodFornecedor),
    RTRIM(DADOSREQF.compCodCondicaoPagamento),
    RTRIM(DADOSREQF.compFrete),
    RTRIM(DADOSREQF.compCodTransportadora),
    compValorFrete

And this is the same query with an external sum which returns de correct value. 这是带有外部总和的相同查询,该查询返回正确的值。

SELECT
    SUM(a.vlr_total_frete),
    a.chave 
FROM
    (SELECT 
        --SUM(CONVERT(FLOAT,    DADOSREQF.compValorFrete)) vlr_total_frete,
        CONVERT(FLOAT,  DADOSREQF.compValorFrete) vlr_total_frete,
        (RTRIM(DADOSFORMF.NUMSOLICITACAO) + RTRIM(DADOSREQF.compCodFornecedor) + RTRIM(DADOSREQF.compCodCondicaoPagamento) +  RTRIM(DADOSREQF.compFrete) + RTRIM(DADOSREQF.compCodTransportadora)) chave
    FROM
        FLUIG.DBO.ML001102 DADOSFORMF WITH(NOLOCK)
    INNER JOIN 
        FLUIG.DBO.ML001105 DADOSREQF WITH(NOLOCK) ON DADOSREQF.DOCUMENTID = DADOSFORMF.DOCUMENTID 
    WHERE
        (RTRIM(DADOSFORMF.NUMSOLICITACAO) + RTRIM(DADOSREQF.compCodFornecedor) + RTRIM(DADOSREQF.compCodCondicaoPagamento) +  RTRIM(DADOSREQF.compFrete) + RTRIM(DADOSREQF.compCodTransportadora)) = '1071002999439000103030F040' 
        AND DADOSFORMF.version = (SELECT MAX(version) 
                                  FROM FLUIG.DBO.ML001105 vr 
                                  WHERE vr.documentid = DADOSFORMF.documentid)
    GROUP BY
        RTRIM(DADOSFORMF.NUMSOLICITACAO),
        RTRIM(DADOSREQF.compCodFornecedor),
        RTRIM(DADOSREQF.compCodCondicaoPagamento),
        RTRIM(DADOSREQF.compFrete),
        RTRIM(DADOSREQF.compCodTransportadora),
        compValorFrete) a
GROUP BY 
    a.chave

And some screenshots of it running on SQL Server 2014. 以及它在SQL Server 2014上运行的一些屏幕截图。

https://i.stack.imgur.com/dcGb4.png https://i.stack.imgur.com/dcGb4.png

https://i.stack.imgur.com/ww4gp.png https://i.stack.imgur.com/Sv33l.png https://i.stack.imgur.com/ww4gp.png https://i.stack.imgur.com/Sv33l.png

Simplified example of what is happening, consider this table: 发生的事情的简化示例,请考虑以下表格:

col1   |   col2
 A     |    10
 A     |    10

This will return one row: 这将返回一行:

SELECT col1, col2
FROM table
GROUP BY col1, col2

col1   |   col2
 A     |    10

Adding an outer SUM will apply to that one row: 添加外部SUM将应用于该行:

SELECT SUM(col2)
FROM (SELECT col1, col2
      FROM table
      GROUP BY col1, col2) A

col1   |   col2
 A     |    10

However, adding a SUM directly will consider both rows and double the value: 但是,直接添加SUM将同时考虑行和值的两倍:

SELECT col1, SUM(col2)
FROM table
GROUP BY col1

col1   |   col2
 A     |    20

You should see 2 records that are duplicates if you run this code. 如果运行此代码,您应该看到2条记录重复。 The reason you are getting 10 and 20 different results is because of these 2 duplicates. 您得到10和20个不同结果的原因是由于这2个重复项。

SELECT 
    SUM(CONVERT(FLOAT,  DADOSREQF.compValorFrete)) vlr_total_frete,
    --CONVERT(FLOAT,  DADOSREQF.compValorFrete) vlr_total_frete,
    (RTRIM(DADOSFORMF.NUMSOLICITACAO) + RTRIM(DADOSREQF.compCodFornecedor) + RTRIM(DADOSREQF.compCodCondicaoPagamento) +  RTRIM(DADOSREQF.compFrete) + RTRIM(DADOSREQF.compCodTransportadora)) chave
FROM
    FLUIG.DBO.ML001102 DADOSFORMF WITH(NOLOCK)
INNER JOIN 
    FLUIG.DBO.ML001105 DADOSREQF WITH(NOLOCK) ON DADOSREQF.DOCUMENTID = DADOSFORMF.DOCUMENTID 
WHERE
    (RTRIM(DADOSFORMF.NUMSOLICITACAO) + RTRIM(DADOSREQF.compCodFornecedor) + RTRIM(DADOSREQF.compCodCondicaoPagamento) +  RTRIM(DADOSREQF.compFrete) + RTRIM(DADOSREQF.compCodTransportadora)) = '1071002999439000103030F040' 
    AND DADOSFORMF.version = (SELECT MAX (version) 
                              FROM FLUIG.DBO.ML001105 vr 
                              WHRE vr.documentid = DADOSFORMF.documentid)
GROUP BY
    RTRIM(DADOSFORMF.NUMSOLICITACAO),
    RTRIM(DADOSREQF.compCodFornecedor),
    RTRIM(DADOSREQF.compCodCondicaoPagamento),
    RTRIM(DADOSREQF.compFrete),
    RTRIM(DADOSREQF.compCodTransportadora)
    /* ,compValorFrete */

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

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