Let's have a look at the picture.
The result is different even though the expression is the same.
Why does this happen?
I have to follow excel result, what should I have do with sql server?
No matter whatever the software is 1+1 will always yeild 2 and if its not you should check you calculation again. see below
SELECT ((4972000.0000) * (1.0000 - 4.4000/100.0000))
/ ((1.0000 + ((36.0000/365.0000)) * (13.0000 / 100.0000)))
RESULT: 4693057.996104
To get the result on upto four decimal places Use ROUND() function.
SELECT ROUND(((4972000.0000) * (1.0000 - 4.4000/100.0000))
/ ((1.0000 + ((36.0000/365.0000)) * (13.0000 / 100.0000))), 4)
RESULT: 4693057.996100
Related
I have a rounding discrepancy in SQL that I could do with a hand resolving.
I have 2 SQL calculations, the first one equals 1.1 and the second 5.65 (see below)
round((sum((monthly_markup)+100) / 100) / sum(monthly_qty),2) as timesby, --equals 1.1
sum(monthly_buy)/sum(monthly_qty) as buy, -- equals 5.65
If I then take those calculations and do calc1 x calc2 it equals 6.21
cast (round(sum(monthly_buy)/sum(monthly_qty) * (sum((monthly_markup)+100) / 100) / sum(monthly_qty),2) as decimal (30,2)), -- equals 6.21 !!
But I am expecting 6.22, as per the below calculation
cast (round((5.65 * 1.1),2) as decimal (30,2)) -- equals 6.22
How can I get my calculation to return 6.22?
Thanks
In case it helps anyone else, I resolved by casting the sum of buy * qty to decimal first.
round((cast(sum(monthly_buy)/sum(monthly_qty) as decimal (5,3)) * round((sum((monthly_markup)+100) / 100) / sum(monthly_qty),2)),2)
Can you explain this error in SQL Server 2014 Express?
SELECT FLOOR(21474837 * 100) -- doesn't work
SELECT FLOOR(214748370 * 10) -- doesn't work
SELECT FLOOR(2147483700) -- works
SELECT FLOOR(21474837 / 0.01) -- works
The number 21474837 is chosen because inferior numbers have no problem neither
That's because FLOOR returns same datatype as the input, which is INT in your case. Add some decimals and it will fix it.
SELECT FLOOR(21474837.00 * 100)
The largest INT number is 2147483647 and 21474837 * 100 = 2147483700, which is 53 too many
Trying to calculate this in SQL Server.
Where c=85, z=4, d=6, and e=4
I tried this
select 85 / 4 * (6 * 4)
but looks like this is wrong any suggest please
Use POWER to handle the exponent:
SELECT 85.0 / (4 * POWER(6, 4))
or
SELECT CAST(85 AS DECIMAL(10, 4)) / (4 * POWER(6, 4))
Or in variable format:
SELECT c / (z * POWER(d, e))
select 85.0 / 4 * power(6,4)
slightly ambiguous formula, does it mean C divided by z then multiplied by d^e - anyway - use SQL x^y = POWER(x,y) and also put 85.0 to make sure floats are used
I am trying to do the following operation in SQL to calculate the weighted rating :
SELECT CTE_3.idProduct,(CTE_3.vote_count / (CTE_3.vote_count + #minimumVotesRequired)) * CTE_3.vote_mean + (#minimumVotesRequired / (CTE_3.vote_count+ #minimumVotesRequired)) * ((SUM(CTE_3.vote_mean)/COUNT(CTE_3.IdProduct))) AS WeightedRating
FROM CTE_3
GROUP BY CTE_3.IdProduct,
CTE_3.vote_count,
CTE_3.vote_mean
ORDER BY idProduct;
But the problem I am facing is that the result is ALWAYS 0.. I tried using Convert(FLOAT,operation) AS WeightedRating but still I am getting a result of 0.
When I manually perform this on a calculator it returns 2.5416666..so I am quite sure that SQL Server is not being able to manage the values I feed to the operation.
Should I do something else than cast?
The values are :
vote_count is 2
vote_mean is 2.5
#minimumVotesRequired is 1
EDIT :
Now the only value after casting everything to float is 2.5 from CTE_3.vote_mean
SELECT CTE_3.idProduct,(CONVERT(FLOAT,CTE_3.vote_count) / (CONVERT(FLOAT,CTE_3.vote_count) + #minimumVotesRequired))
* CONVERT(FLOAT,CTE_3.vote_mean) +
(#minimumVotesRequired / (CONVERT(FLOAT,CTE_3.vote_count)+ #minimumVotesRequired))
* (SUM(CONVERT(FLOAT,CTE_3.vote_mean)))/COUNT(CTE_3.IdProduct)) AS WeightedRating
FROM CTE_3
GROUP BY CTE_3.IdProduct,
CTE_3.vote_count,
CTE_3.vote_mean
ORDER BY idProduct;
Any suggestion in what am I missing?
If CTE_3.vote_count type is int or #minimumVotesRequired type is int, then you are getting the truncated int value. Make sure those are floats, or cast them as floats before doing your division.
Also, don't forget that COUNT is an integer function. You will want to cast the result of your COUNT as a float as well.
Using T-SQL and Microsoft SQL Server I would like to specify the number of decimal digits when I do a division between 2 integer numbers like:
select 1/3
That currently returns 0. I would like it to return 0,33.
Something like:
select round(1/3, -2)
But that doesn't work. How can I achieve the desired result?
The suggestions from stb and xiowl are fine if you're looking for a constant. If you need to use existing fields or parameters which are integers, you can cast them to be floats first:
SELECT CAST(1 AS float) / CAST(3 AS float)
or
SELECT CAST(MyIntField1 AS float) / CAST(MyIntField2 AS float)
Because SQL Server performs integer division. Try this:
select 1 * 1.0 / 3
This is helpful when you pass integers as params.
select x * 1.0 / y
It's not necessary to cast both of them. Result datatype for a division is always the one with the higher data type precedence. Thus the solution must be:
SELECT CAST(1 AS float) / 3
or
SELECT 1 / CAST(3 AS float)
use
select 1/3.0
This will do the job.
I understand that CASTing to FLOAT is not allowed in MySQL and will raise an error when you attempt to CAST(1 AS float) as stated at MySQL dev.
The workaround to this is a simple one. Just do
(1 + 0.0)
Then use ROUND to achieve a specific number of decimal places like
ROUND((1+0.0)/(2+0.0), 3)
The above SQL divides 1 by 2 and returns a float to 3 decimal places, as in it would be 0.500.
One can CAST to the following types: binary, char, date, datetime, decimal, json, nchar, signed, time, and unsigned.
Looks like this trick works in SQL Server and is shorter (based in previous answers)
SELECT 1.0*MyInt1/MyInt2
Or:
SELECT (1.0*MyInt1)/MyInt2
Use this
select cast((1*1.00)/3 AS DECIMAL(16,2)) as Result
Here in this sql first convert to float or multiply by 1.00 .Which output will be a float number.Here i consider 2 decimal places. You can choose what you need.
If you came here (just like me) to find the solution for integer value, here is the answer:
CAST(9/2 AS UNSIGNED)
returns 5
I was surprised to see select 0.7/0.9 returning 0.8 in Teradata given they're already as floats/decimal numbers! I had to do cast(0.7 as float) to get the output that I was after.
When using literals, the best way is to "tell" SQL
which type you mean.
if you want a decimal result, add decimal point ".0" to your numbers:
SELECT 1.0 / 3.0
Result
0.333333
if you want a float (real) result, add "e0" to your numbers:
SELECT 1e0 / 3e0
Result
0.333333333333333