English 中文(简体)
2005年:在增加SUM(SUM)时,数字表示变动表
原标题:SQL Server 2005: Scale of numeric expression changes when adding SUM()

我有一个称为“Jrl”的表格,分为三栏:

code  AS varchar(15)
total AS numeric(13,2)
rem   AS numeric(13,4)

为了这一论点,让我们假设,表中只有一行,数值为001、400.00和52.1745。

3. 审议以下问题:

SELECT code, total - rem
 FROM Jrl

它用001和归还一行。 这是正确的。

如果我把问询改变如下(事实上,我需要在我的法典中提问):

SELECT code, SUM(total) - SUM(rem)
 FROM Jrl
 GROUP BY code

它用001和347.83(即,比额表2而不是4)返回一行。

现在,根据的文件,数字表达的类型应当为数字(164),这显然是t. (I在2005和2008年R服务器上相同)。

谁能告诉我那里发生什么情况?

Btw。 我确实找到了工作,但我不喜欢,这就是我为什么要提出这个问题。 工作是增加一个明确的内容:

SELECT code, CAST(SUM(total) AS numeric(13,4)) - SUM(rem)
 FROM Jrl
 GROUP BY code
最佳回答

(1) 请介绍这一文字并阅读我的评论。

2) 我希望这一答案将有助于你。

3) SUM(SUM)-SUM(SUM)的精确度为2,因为您选择的是一至一(SUM( Total)SUM(rem),然后再细分(SUM( Total)-SUM(rem))。

4) 我建议使用<代码>SlectT t.code, SUM(t. Total - t.rem) AS diff ...(第一分录,然后是SUM)。

5) 您可阅读我对这个问题的答复:。 纽美数据类型truncating Value?:

DECLARE @Test TABLE(
    code  varchar(15),
    total numeric(13,2),
    rem   numeric(13,4)
);

INSERT  @Test (code, total, rem)
VALUES  ( 001 , 11.78, 5.6789);

--Test [1]
SELECT  dt.*,
        SQL_VARIANT_PROPERTY(dt.diff,  BaseType ) AS diff_BaseType,
        SQL_VARIANT_PROPERTY(dt.diff,  Precision ) AS diff_Precision,
        SQL_VARIANT_PROPERTY(dt.diff,  Scale ) AS diff_Scale
FROM
(
        SELECT  t.code, t.total - t.rem AS diff
        FROM    @Test t
) dt;

/*
Operation: e1 - e2
Result precision: max(s1, s2) + max(p1-s1, p2-s2) + 1 = max(2,4) + max(13-2, 13-4) + 1 = 4 + 11 + 1 = 16
Result scale: max(s1, s2) = max(2, 4) = 4
*/

--Test [2]
SELECT  dt.*,
        SQL_VARIANT_PROPERTY(dt.diff,  BaseType ) AS diff_BaseType,
        SQL_VARIANT_PROPERTY(dt.diff,  Precision ) AS diff_Precision,
        SQL_VARIANT_PROPERTY(dt.diff,  Scale ) AS diff_Scale
FROM
(
        SELECT  t.code, SUM(t.total - t.rem) AS diff
        FROM    @Test t
        GROUP BY t.code
) dt;

/*
Operation: SUM(e1 - e2)
Result precision: 38--For SUM function, I think (it s just a hipotese), SQL Server choose the maximum precision to prevent the overflow error
                    Argument:
                    DECLARE @t TABLE (Col NUMERIC(2,1)); INSERT @t VALUES (1);
                    SELECT  SQL_VARIANT_PROPERTY(SUM(t.Col),  Precision ) FROM @t t;
                    Result: precision = 38 (maximum DECIMAL/NUMERIC precision)
Result scale: the same scale as (e1-e2)= 4 (please see Test [1])
*/

--Test [3]
SELECT  dt.*,
        SQL_VARIANT_PROPERTY(dt.SUM_total,  BaseType )  AS SUM_total_BaseType,
        SQL_VARIANT_PROPERTY(dt.SUM_total,  Precision ) AS SUM_total_Precision,
        SQL_VARIANT_PROPERTY(dt.SUM_total,  Scale )     AS SUM_total_Scale,

        SQL_VARIANT_PROPERTY(dt.SUM_rem,  BaseType )    AS SUM_rem_BaseType,
        SQL_VARIANT_PROPERTY(dt.SUM_rem,  Precision )   AS SUM_rem_Precision,
        SQL_VARIANT_PROPERTY(dt.SUM_rem,  Scale )       AS SUM_rem_Scale,

        SQL_VARIANT_PROPERTY(dt.diff,  BaseType )       AS diff_BaseType,
        SQL_VARIANT_PROPERTY(dt.diff,  Precision )      AS diff_Precision,
        SQL_VARIANT_PROPERTY(dt.diff,  Scale )          AS diff_Scale
FROM
(
        SELECT  t.code, 
                SUM(t.total) AS SUM_total, SUM(t.rem) AS SUM_rem, SUM(t.total) - SUM(t.rem) AS diff
        FROM    @Test t
        GROUP BY t.code
) dt;

/*
Operation: SUM(total) (<> e1 + e2 + ...)
Result precision: 38--I think SQL Server choose the maximum precision to prevent the overflow error
Result scale: the same precision as total= 2
*/


/*
Operation: SUM(rem) (<> e1 + e2 + ...)
Result precision: 38--I think SQL Server choose the maximum precision to prevent the overflow error
Result scale: the same precision as rem= 4
*/

/*
Operation: SUM(total) - SUM(rem) = e1 - e2
Result precision: max(s1, s2) + max(p1-s1, p2-s2) + 1 = max(2,4) + max(38-2, 38-4) + 1 = 4 + 36 + 1 = 41 
but max. precision is 38 so result precision = 38

Calculated result scale: max(s1, s2) = 4 
but because the real precision for result (41) is greater than maximum precision (38)
SQL Server choose to decrease the precision of the result to 2 (please see Test [3] - diff_Scale).
In this case (the real precision for result is greater than maximum precision) I think the 
expression for result s precision is max(s1, s2) - (real precision - maximum precision) + 1 = 4 - (41 - 38) + 1 = 4 - 3 + 1 = 2
For example you could try to modify the definition of total column to `total numeric(13,1)` 
and you will see that the precision for SUM(total) - SUM(rem) becomes 4 - 4(4+37+1=42) + 1 = 1
*/

成果:

--Test [1] SELECT t.code, t.total - t.rem AS diff
code diff   diff_BaseType  diff_Precision diff_Scale
---- ------ -------------- -------------- ----------
001  6.1011 numeric        16             4

--Test [2] SELECT t.code, SUM(t.total - t.rem) AS diff
code diff   diff_BaseType diff_Precision diff_Scale
---- ------ ------------- -------------- ----------
001  6.1011 numeric       38             4

--Test [3] SELECT t.code, ..., SUM(t.total) - SUM(t.rem) AS diff
code SUM_total SUM_rem diff SUM_total_BaseType SUM_total_Precision SUM_total_Scale SUM_rem_BaseType SUM_rem_Precision SUM_rem_Scale diff_BaseType diff_Precision diff_Scale
---- --------- ------- ---- ------------------ ------------------- --------------- ---------------- ------------------------------- ------------- -------------- ----------
001  11.78     5.6789  6.10 numeric            38                  2               numeric          38                4             numeric       38             2
问题回答

由于<代码>,<<>全印(13,2)>

由于你正在以不同的精准程度分出两个领域的总和,因此,l服务器的精度是最小的。

如果是,

 create table jrl2(
code  varchar(15),
total numeric(13,4),
rem   numeric(13,4)
)
insert into jrl2 values ( 001 , 400.00 , 52.1745)

select * from jrl2
SELECT code, total - rem  FROM Jrl

SELECT code, SUM(total) - SUM(rem)
 FROM Jrl2
 GROUP BY code

页: 1





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...