TSQL Sum is giving wrong result - sql

I want to get sum of data but I am getting wrong result
Example 1
Example 2
Result when doing sum
AS you can see from Example 1 - where p_key is 11020145101617761 and LC_Amount is 8.4 , 168 , -176.4 the sum of this is 0
similarly in Example 2 - where p_key is 1102014510615767 and LC_amount is
-571067.53, 543873.84 , 27193.69 the sum of this is also 0
but in the result when I do group by with p_key , I am not getting 0
I don't understand what is the reason behind this.

It's an example of IEEE-754 rounding errors. Note the numbers are all very close to zero, but juuuuuust off, see the exponent.
Wrap your SUM in ROUND():
SELECT ROUND( SUM( LC_Amount ), 10 )
...should do it.

This occurs because float is not true exact numeric type.
Try to use DECIMAL(10,2) type to avoid round errors.
There is a lot info about float type in the web.

Related

Calculate average of a row in SQL Server

I have the below table..the percent column is of type nvarchar
Data Percent1 Percent2 Percent3
1 3% 4% 6%
2 6% 8% 7%
3 8% 6% 8%
I have to calculate the Avg per line so I get results like
Data Avg
1 4.33%
I was trying to convert the %column into decimal so I can apply the average function
select
Case WHEN Isnumeric([Percent1]) = 1
THEN CONVERT(DECIMAL(18,2),Replace([Percent1],'%',''))
ELSE 0 END AS Percent1
from DashboardData
but I am just getting 0 values..I am guessing the outer function is running before the inner for some reason. Can someone please tell me how I can achieve this.
I know the IsNumeric function will make it 0 but I tried it before that and I was getting an exception that type is not a number.
Thanks
SELECT ISNUMERIC('3%') will return 0, as will all the rest of your values, so your else condition will always be the result.
Just drop the %
select
data,
(replace(Percent1,'%','') + replace(Percent2,'%','') + replace(Percent3,'%','')) * 1.0 / 3
Note, if any of these values are NULL you need to account for that because NULL + anything IS NULL.
Also, you don't want to lean on ISNUMERIC too heavy... it can produce some results you probably aren't expecting
select
ISNUMERIC('$') --money which is a numeric value
,ISNUMERIC('1e4') --scientific notation
,ISNUMERIC('45D-1') --old scientific notation
,ISNUMERIC('.') --just the decimal portion of a float / decimal
Is this what you want?
select dd.*, s.average
from dashboarddata dd cross apply
(select avg(try_convert(numeric(10, 2), replace(pc, '%', ''))) as average
from values (percent1), (percent2), (percent3)) as v(pc)
) s;

Redshift division result does not include decimals

I'm trying to do something really quite basic to calculate a kind of percentage between two columns in Redshift. However, when I run the query with an example the result is simply zero because the decimals are not being covered.
code:
select 1701 / 84936;
Output:
I tried :
select cast(1701 / 84936 as numeric (10,10));
but the result was 0.0000000000.
How could I solve this silly thing?
It is integer division. Make sure that at least one argument is: NUMERIC(accurate data type)/FLOAT(caution: it's approximate data type):
/ division (integer division truncates the result)
select 1701.0 / 84936;
-- or
SELECT 1.0 * 1701 / 84936;
-- or
SELECT CAST(1701 AS NUMERIC(10,4))/84936;
DBFiddle Demo
When mixing data types the order counts
Note that the order of the elements in a math expression counts for the data type of the result.
Let's assume that we intend to calculate the percentage unit_sales/total_sales where both columns (or numbers) are integers.
See and try with this code here.
-- Some dummy table
drop table if exists sales;
create table sales as
select 3 as unit_sales, 9 as total_sales;
-- The calculations
select
unit_sales/total_sales*100, --> 0 (integer)
unit_sales/total_sales*100.0, --> 0.0 (float)
100.0*unit_sales/total_sales --> 33.3 (float and expected result)
from sales;
The output
0 | 0.0 | 33.33
The first column is 0 (integer) because of 3/9=0 in an integer division.
The second column is 0.0 because SQL first got the integer 0 (3/9), and later, SQL converts it to float in order to perform the multiplication by 100.0.
The expected result.
The non-integer 100.0 at the beginning of the expression force a non-integer calculation.

Counting the number of digits in column

Here is my code
select len(cast(code as float)),code
from tbl1
where code is not null
and this is the output:
I want a count of digits in the code column.
I don't understand why the last one is counted as 12 and not 8?
Cast it as an int instead:
select len(cast(code as int)), code
from tbl1
where code is not null;
Presumably, some sort of decimal values are getting counted.
Get the number's power of 10 and add 1. This works either if ints or reals to count the number of digits of the whole number part (note using LOG10 only works on positive numbers so I have applied ABS to get around this issue, may not be required for your data):
SELECT code, CASE WHEN Number = 0 THEN 1
ELSE FLOOR(LOG10(ABS(code))) + 1 AS NDigits
FROM tbl1

Divisor coming as null

I Have to find the percentage difference between two numbers.
i am using the formula A-B/(A+B/2)
I.e
select round( ( A-B)/(A+B/2)) from dual;
where a and b are two numbers. Now if a and b is 0 then the divisor comes as zero. In this case what can be done to avoid error ? what is the percentage difference between two equal numbers or 0 ?

How do I calculate percentages with decimals in SQL?

How can i convert this to a decimal in SQL? Below is the equation and i get the answer as 78 (integer) but the real answer is 78.6 (with a decimal) so i need to show this as otherwise the report won't tally up to 100%
(100 * [TotalVisit1]/[TotalVisits]) AS Visit1percent
Try This:
(100.0 * [TotalVisit1]/[TotalVisits]) AS Visit1percent
convert(decimal(5,2),(100 * convert(float,[TotalVisit1])/convert(float,[TotalVisits]))) AS Visit1percent
Ugly, but it works.
CAST(ROUND([TotalVisit1]*100.0/[TotalVisits],2)as decimal(5,2)) as Percentage
Not ugly and work better and fast , enjoy it!
At least in MySQL (if it helps), if you want to use float numbers you had to use a type float field, not the regular int fields.
Just add a decimal to the 100
(100.0 * [TotalVisit1]/[TotalVisits]) AS Visit1percent
this forces all processing to happen in floats... if you want the final output as text, and truncated for display to only one decimal place, use Str function
Str( 100.0 * [TotalVisit1]/[TotalVisits], 4, 1 ) AS Visit1percent
This works perfectly for me:
CAST((1.0 * SUM(ColumnA) /COUNT(ColumnB)) as decimal(5,2))
Hope it helps someone out there in the world.
Its probably overkill to do this, but you may wish to consider casting all values to floats to ensure accuracy at all phases.
(100.0 * ( [TotalVisit1]+0.0 )/([TotalVisits]+0.0) ) as Visit1percent
Note, you really need to put code in here for the case that TotalVisits == 0 or you will get a division by 0 answer.
SELECT(ROUND(CAST(TotalVisit1 AS DECIMAL)/TotalVisits,1)) AS 'Visit1percent'
This will return a decimal and the ROUND will round it to one digit. So in your case you would get 76.6. If you don't want any digits change the 1 to 0 and if you want two digits change it to 2.
Try with this, no round and str or Over(). i found this as a simpler way to do it.
cast((count(TotalVisit1) * 100.0) /TotalVisits as numeric(5,3)) as [Visit1percent]
You can change the number of decimal points as you wish to
e.g. numeric(5,2) or numeric(5,4)
This might not address you issue directly, but when you round a set of numbers for display you're never guaranteed to get numbers that add to 100 unless you take special precautions. For example, rounding 33.33333, 33.33333 and 33.33333 is going to leave you one short on the sum, so the logical thing to do is to modify the percentage for the largest value in the set to take account of any difference.
Here's a way of doing that in Oracle SQL using analytic functions and a subquery factoring (WITH) clause to generate sample data.
with data as (select 25 num from dual union all
select 25 from dual union all
select 25 from dual)
select num,
case
when rnk = 1
then 100 - sum(pct) over (order by rnk desc
rows between unbounded preceding
and 1 preceding)
else pct
end pct
from
(
select num,
round(100*ratio_to_report(num) over ()) pct,
row_number() over (order by num desc) rnk
from data
)
/
NUM PCT
---------------------- ----------------------
25 33
25 33
25 34
In ms Access You can use the SQL function ROUND(number, number of decimals), It will round the number to the given number of decimals:
ROUND((100 * [TotalVisit1]/[TotalVisits]),1) AS Visit1percent