Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Problem with Round Expand / Collapse
Author
Message
Posted Wednesday, July 23, 2014 10:57 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 1:31 PM
Points: 3, Visits: 26
I have the following problem with ROUND. When doing the calculation for each value by a percentage using round, the sum of the result does not equal the sum of the values ​​for the percentage (also using round).

IF OBJECT_ID('Tempdb..#Redondeo') IS NOT NULL DROP TABLE #Redondeo
Create Table #Redondeo (Orden int Identity(1,1),Valores money)
Insert into #Redondeo
Select 71374.24 Union Select 16455.92
Union Select 56454.20 Union Select 9495.18
Union Select 23894.20

Select *,Round(Valores*.21,2) as IVA
from #Redondeo
Compute sum(Valores),sum(Round(Valores*.21,2))

RESULT = 177673.74; 37311.48

Select sum(Valores),round(sum(Valores *.21),2) as IVA
from #Redondeo

RESULT: 177673.74; 37311.49

Post #1595599
Posted Wednesday, July 23, 2014 11:23 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 1:44 PM
Points: 4,041, Visits: 9,188
The problem is that one query is summing the values before rounding and the other one is summing the values after rounding. You need to define which option do you want.
This should make it clear.
Select	sum(Valores),
sum(round(Valores *.21,2)) as IVA, --Round then sum
Round(sum(Valores*.21),2) AS IVA2, --Sum then round
sum(Valores*.21) AS IvaSinRedondear --Just Sum
from #Redondeo




Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1595611
Posted Wednesday, July 23, 2014 12:20 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 1:31 PM
Points: 3, Visits: 26
Thanks Luis, I'll try your suggestions
Post #1595650
Posted Wednesday, July 23, 2014 1:31 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 1:31 PM
Points: 3, Visits: 26
To clarify the problem. The initial calculation is: Round (177673.74 * 0.21,2) = 37311.49
Then I need to make the opening of each item and the corresponding calculation, which must match the sum to the initial calculation, but it gives 0.01 difference:

Ítems; Valores; Round (Valores*0.21,2)
Item 1; 9,495.18; 1,993.99
Item 2 16,455.92 3,455.74
Item 3 23,894.20 5,017.78
Item 4 56,454.20 11,855.38
Item 5 71,374.24 14,988.59
37,311.48

But I think there is no solution can only be set in any of the items the difference
Post #1595689
Posted Wednesday, July 23, 2014 2:07 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 8:40 AM
Points: 1,943, Visits: 20,149
just a thought...but appears to work, but maybe I am not entirely clear on the issue


IF OBJECT_ID('Tempdb..#Redondeo') IS NOT NULL
DROP TABLE #Redondeo

CREATE TABLE #Redondeo (
Orden INT Identity(1, 1)
, Valores MONEY
)

INSERT INTO #Redondeo
SELECT 71374.24 UNION
SELECT 16455.92 UNION
SELECT 56454.20 UNION
SELECT 9495.18 UNION
SELECT 23894.20

SELECT Orden
, Valores
,round (Valores * 0.21,2) as rnd
FROM #Redondeo


SELECT Orden
, Valores
,round (Valores * 0.21,2) as rnd
into #testsum
FROM #Redondeo

SELECT SUM(rnd) AS sumbyrow
FROM #testsum

SELECT SUM(ROUND(Valores * 0.21, 2)) AS sumrnd
FROM #Redondeo




______________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
Post #1595705
Posted Friday, July 25, 2014 3:04 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:44 PM
Points: 7,179, Visits: 15,764
mballico (7/23/2014)
To clarify the problem. The initial calculation is: Round (177673.74 * 0.21,2) = 37311.49
Then I need to make the opening of each item and the corresponding calculation, which must match the sum to the initial calculation, but it gives 0.01 difference:

Ítems; Valores; Round (Valores*0.21,2)
Item 1; 9,495.18; 1,993.99
Item 2 16,455.92 3,455.74
Item 3 23,894.20 5,017.78
Item 4 56,454.20 11,855.38
Item 5 71,374.24 14,988.59
37,311.48

But I think there is no solution can only be set in any of the items the difference


Rounding errors will occur in any scenario like this. It's not that SQL Server's ROUND() function has an issue, the use of rounding lends itself to rounding errors.

http://www.investopedia.com/terms/r/rounding-error.asp


----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Post #1596423
Posted Wednesday, July 30, 2014 6:53 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 2:15 AM
Points: 996, Visits: 3,088


CREATE TABLE #t (Valores MONEY);
GO
INSERT INTO #t SELECT 1.1;
GO 6

SELECT
SUM(ROUND(Valores, 0)) AS SumRound,
ROUND(SUM(Valores), 0) AS RoundSum
FROM
#t;

DROP TABLE #t;





The SQL Guy @ blogspot

@SeanPearceSQL

About Me


  Post Attachments 
Round Error.PNG (36 views, 4.76 KB)
Post #1597694
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse