Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7877633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:25:48+00:00 2026-06-03T03:25:48+00:00

I have a table called Jrl with three columns: code AS varchar(15) total AS

  • 0

I have a table called “Jrl” with three columns:

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

Let us assume for the sake of the argument, that there is just one row in the table with values ‘001’, 400.00 and 52.1745.

Consider the following query:

SELECT code, total - rem
 FROM Jrl

It returns one row with ‘001’ and 347.8255. This is correct.

If I change the query as follows (which is in fact the query I need in my code):

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

It returns one row with ‘001’ and 347.83 (that is, with scale 2 instead of 4).

Now according to the documentation at http://msdn.microsoft.com/en-us/library/ms190476%28v=sql.90%29.aspx, the type of the numeric expression (subtraction) should be numeric(16,4), which it obviously isn’t. (I get the same behavior on SQL Server 2005 and 2008 R2.)

Can someone enlighten me as to what is happening there?

Btw. I did find a workaround, but I don’t like it which is why I am posting this question. The workaround would be to add an explicit cast:

SELECT code, CAST(SUM(total) AS numeric(13,4)) - SUM(rem)
 FROM Jrl
 GROUP BY code
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:25:49+00:00Added an answer on June 3, 2026 at 3:25 am

    1) Please run this script and read my comments.

    2) I hope this answer will help you.

    3) The precision for SUM()-SUM() is 2 because you choose first to sum(SUM(total) and SUM(rem)) and then to subtract (SUM(total) - SUM(rem)).

    4) My advise is to use SELECT t.code, SUM(t.total - t.rem) AS diff ... (first subtract and then SUM).

    5) You may read my answer to this question SQL Numeric data type 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
    */
    

    Results:

    --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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my oracle database I have table called PERSON with columns code, surname, forename.
I have table called emp . The table contains three columns empid , ename
We have a table called table1 ... (c1 int indentity,c2 datetime not null,c3 varchar(50)
I have a table called Movies i want to select some columns from it
I have table called Reporting with following columns OutletId CampaignId ItemId Qty 10 1
Assuming I have table called events with the columns INT id DATETIME start_time DATETIME
I have table name called Person with following column names P_Id(int), LastName(varchar), FirstName (varchar).
I have a table called Foo and I have two columns: Lorem and Ipsum,
I have table called product review with columns productreviewid , productid , shopperid ,
I have table called articles_tags which have two columns: article_id tag_id (has_and_belongs_to_many association) (I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.