SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Problem with Round


Problem with Round

Author
Message
mballico
mballico
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
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
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16170 Visits: 19059
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.
General Disclaimer:
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?


How to post data/code on a forum to get the best help: Option 1 / Option 2
mballico
mballico
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 26
Thanks Luis, I'll try your suggestions
mballico
mballico
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
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
J Livingston SQL
J Livingston SQL
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5382 Visits: 35269
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

Matt Miller (4)
Matt Miller (4)
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12087 Visits: 18567
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?
Sean Pearce
Sean Pearce
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1758 Visits: 3432


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
Attachments
Round Error.PNG (44 views, 4.00 KB)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search