I want to get value in decimal while calculating averave of column values in select query. For that I have used below query. But the value comes as 3.0 instead of 3.6. Is there any solution for that?
SELECT P.ANSW_ONE,P.ANSW_TWO,P.ANSW_THREE,P.ANSW_FOUR,P.ANSW_FIVE,
CAST(((P.ANSW_ONE+P.ANSW_TWO+P.ANSW_THREE+P.ANSW_FOUR+P.ANSW_FIVE)/5) AS DECIMAL(10,1)) AS ANSW_AVG
FROM FEEDBACK P
CAST the whole SUM instead of the result and use 5.0 just to force it to be decimal instead of integer.
SELECT P.ANSW_ONE,
P.ANSW_TWO,
P.ANSW_THREE,
P.ANSW_FOUR,
P.ANSW_FIVE,
(CAST((P.ANSW_ONE+P.ANSW_TWO+P.ANSW_THREE+P.ANSW_FOUR+P.ANSW_FIVE) DECIMAL(10,1) /5.0)) AS ANSW_AVG
FROM FEEDBACK P
Try This
SELECT
P.ANSW_ONE,
P.ANSW_TWO,
P.ANSW_THREE,
P.ANSW_FOUR,
P.ANSW_FIVE,
CAST(CAST(P.ANSW_ONE+P.ANSW_TWO+P.ANSW_THREE+P.ANSW_FOUR+P.ANSW_FIVE AS DECIMAL(10,1))/5 AS DECIMAL(10,1)) AS ANSW_AVG
FROM FEEDBACK P
Cast the sum before dividing by 5 and then again cast the result
Related
I am trying to calculate the average ice creams a kid will have during summer.
I want the result to have 2 decimals.
Query:
Select day, (sum(ice_cream_cones) *1.0)/(select count(kids))
From t1 Group by 1
The result I get is something like 1.0003. I only want 2 decimal points.
Any suggestions?
You can do this with round
Select day, round((sum(ice_cream_cones) *1.0)/(select count(kids) ), 2)
From t1
Assuming your source data are integers...
SUM(ice_cream_cones) * 100 / COUNT(kids) / 100.0
Or, cast your existing calculation to a decimal?
CAST(<calc> AS DECIMAL(8,2))
I would like to make a report that will show the average grade for different tasks.
I am having trouble with how to get the averages. I need to figure out how to convert the grades to floats so that I can take the averages. The grades sometimes have non-numeric or null values, although most values look like "2.0" or "3.5". I can exclude anything that is non-numeric.
This is what I have so far:
Select
GradingScores.task As task,
Avg(Cast((SELECT GradingScores.score WHERE GradingScores.score LIKE '%[^0-9]%')As float)) As averages
From
GradingScores
I am using FlySpeed SQL Query.
You could try using IsNumeric
Select
GradingScores.task As task,
Avg(Cast(GradingScores.score as float) As averages
From
GradingScores where IsNumeric(GradingScores.score) = 1
Just simply convert() and use isnumeric() function in T-SQL
select avg(convert(float,score))
from GradingScores
where isnumeric(score)=1
here's what worked for me:
SELECT AVG(CAST(columnName AS FLOAT)) FROM tableName;
I am working on SQL Server. I have a table that has an int column HalfTimeAwayGoals and I am trying to get the AVG with this code:
select
CAST(AVG(HalfTimeAwayGoals) as decimal(4,2))
from
testtable
where
AwayTeam = 'TeamA'
I get as a result 0.00. But the correct result should be 0.55.
Do you have any idea what is going wrong ?
select
AVG(CAST(HalfTimeAwayGoals as decimal(4,2)))
from
testtable
where
AwayTeam = 'TeamA'
If the field HalfTimeAwayGoals is an integer, then the avg function does an integer average. That is, the result is 0 or 1, but cannot be in between.
The solution is to convert the value to a number. I often do this just by multiplying by 1.0:
select CAST(AVG(HalfTimeAwayGoals * 1.0) as decimal(4, 2))
from testtable
where AwayTeam = 'TeamA';
Note that if you do the conversion to a decimal before the average, the result will not necessary have a scale of 4 and a precision of 2.
Can you try dividing by 1.00 to convert the integer into decimal?
select
AVG(HalfTimeAwayGoals/1.00) as average
from
testtable
where
AwayTeam = 'TeamA'
I used the /1.00 OR /1.0000 trick on various databases to do the job. Unfortunately, I don't have access MS-SQL to try it. The division of integer by 1.00 will change the integer to decimal.
SELECT AVG(integerfield/1.00) FROM table
In MySQL
SELECT AVG(integerfield) FROM table
gives me 4 decimal points.
If I do
SELECT AVG(integerfield/1.00) FROM table
I get 8 decimal points.
I have a query like this:
Select Customer,JobType,sum(SThours),
sum(OThours),SortMonth,
str((sum(OThours)/sum(SThours)),5,2)AS Ratios
from #data
group by Customer,JobType,SortMonth
Sum (SThours): 688
sum(OThours): 618
The ratio should be 618/688=0.90 but my result is 1.00
I am new to SQL, and I need help. Thanks.
Try casting one or other side of the division to a float - to force floating point rather than integer division.
SELECT
Customer,
JobType,
sum(SThours),
sum(OThours),
SortMonth,
str((CAST(sum(OThours) AS FLOAT)/sum(SThours)),5,2) AS Ratios
FROM
#data
GROUP BY
Customer,
JobType,
SortMonth
Try casting the number to decimal
SELECT
Customer,
JobType,
sum(SThours),
sum(OThours),
SortMonth,
str((CAST(sum(OThours) AS DECIMAL)/sum(SThours) ),5,2) AS Ratios
FROM
#data
GROUP BY
Customer, JobType, SortMonth
Try this sample fiddle
it is simple example showing getting decimal values
Your problem is that you're performing an integer division, which will always result in an integer value in return.
You should cast the sums to a floating point numeric value (decimal should work) before dividing them.
I have a problem with sql(maths).
I have a total payable given to vendor which is 33.333, and I need to divide the amount with two users. So I have select
select (16.666 * 2) from dual which gives me 33.332 that is .1 less than the total amount I have to give.
If I have this sql
select (16.667 * 2) from dual, then it gives me 33.334 which .1 greater than 33.333.
How can I divide the total amount which I could equally distribute?
Thanks
I'm not sure from where are you executing your query, but it works here (SQLDeveloper, 10g):
SELECT (33.333 / 2) FROM dual;
16,6665
SELECT (16.6665 * 2) FROM dual;
33,333
Do it the other way around:
select 33.333/2
You are most likely working with the wrong column type. You should be using DECIMAL instead of e.g. FLOAT.
Here is a good summary: http://lists.mysql.com/mysql/189592
Depending on the SQL standard you are using the type can be MONEY, DECIMAL or NUMBER.