Calculate an average as a percent? - sql

This is a simple problem and I'm surprised I couldn't find an answer to it.
All I need, is to calculate the average of a column and have the output as a percent.
Select round(avg(amount)::numeric, 2)
From Table
All I need is to figure out how to make that a percent, right now it comes out as 12345.67, how would I convert that output to a percentage in the query?

Maybe you need something like this:
select avg(amount) * 100 / sum(amount)
from Table
Or you can add a percent sign to it like this:
select CAST(avg(amount) * 100 / sum(amount)as VARCHAR(max)) + '%' AS Perc
from Table
or you can just concatenate a sign to your query:
Select CAST(round(avg(amount)::numeric, 2)as VARCHAR(max)) + '%' AS Perc
From Table

Result / Total * 100 = Average
In your case, the Result is: avg(amount)::numeric
and the Total is whatever the maximum that amount could be.

Your best bet would be to use whatever tools are available to you. But there is a way you can do it as a query.
Try getting the average divided by the sum multiplied by 100.
So you want the average, like below and divided by the sum, and multiplied by 100 to get 100% of the sum.
select avg(amount) / sum(amount) * 100

Related

Want decimal result Amazon Redshift

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))

Average of minimum and maximum in SQL

I am trying to find the average of the minimum and maximum in SQL, but my code doesn't work.
SELECT AVG(MIN(Num) + MAX(Num))
FROM Table1;
Divide by 2:
SELECT (MIN(Num) + MAX(Num)) / 2
FROM Table1;

Calculate percentage between two columns in SQL Query as another column

I have a table with two columns, number of maximum number of places (capacity) and number of places available (availablePlaces)
I want to calculate the availablePlaces as a percentage of the capacity.
availablePlaces capacity
1 20
5 18
4 15
Desired Result:
availablePlaces capacity Percent
1 20 5.0
5 18 27.8
4 15 26.7
Any ideas of a SELECT SQL query that will allow me to do this?
Try this:
SELECT availablePlaces, capacity,
ROUND(availablePlaces * 100.0 / capacity, 1) AS Percent
FROM mytable
You have to multiply by 100.0 instead of 100, so as to avoid integer division. Also, you have to use ROUND to round to the first decimal digit.
Demo here
The following SQL query will do this for you:
SELECT availablePlaces, capacity, (availablePlaces/capacity) as Percent
from table_name;
Why not use a number formatting function such as format_number (or an equivalent one in your database) to format a double as a percentage? This example is generalized. The returned value is a string.
WITH t
AS
(
SELECT count(*) AS num_rows, count(foo) as num_foo
FROM mytable
)
SELECT *, format_number(num_foo/num_rows, '#.#%') AS pct_grade_rows
FROM t
This avoids the use of round and multiplying the numerator by 100.

Percentage calculation of specific number of row over total

I'm looking for SELECT statement to calculate percentage of specific number of row over the total number of rows.
For example; lets say i have a FRUIT table like this;
I want to calculate a percentage of rows that its name is not peach, over the total number of rows. I try this statement :
SELECT CAST((select count(name) from fruit WHERE name !='peach')
as FLOAT) /
(select count(name)from fruit)*100.0 as percentage ;
but it doesn't give me correct number. I also need a statement that i can calculate percentage of each fruit by grouping them with Group by function
I'm very new at SQL and i keep trying but cant find the right syntax. Please help me.
I think the easiest way to do this is using conditional aggregation with average:
select avg(case when fruit <> 'peach' then 100.0 else 0.0 end)
from fruits;
In Postgres, you can use the shorthand:
select 100*avg((fruit <> 'peach')::int)
from fruits;

Calculate weighted average in single query

Example data:
table A
part rating numReviews
A308 100 7
A308 98 89
I'm trying to get the average rating for the above data.
What it needs to be is the sum of rating*numReviews for each line divided by the total numReviews
This is what I'm trying but it's giving incorrect result (49.07, should be 98.15):
select part,
cast((AVG(rating*numReviews)/sum(numReviews)) as decimal(8,2)) as rating_average
from A group by part order by part
Can this be done in a single query? I'm using SQL Server
Just go back to the definition of weighted average, so use sum()s and division:
select part, sum(rating * numreviews) / sum(numreviews) as rating_average
from a
group by part
order by part;
You can convert this to a decimal if you like:
select part,
cast(sum(rating * numreviews) / sum(numreviews) as decimal(8, 2)) as rating_average
from a
group by part
order by part;