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;
Related
Apologies if this has been asked. For what I thought was a relatively simple question, I couldn't seem to find the answer in my searches.
linesInserted and linesDeleted are two columns in the table. I'm trying to return rows where linesInserted + linesDeleted >= 200. The query I have is
SELECT *, (linesInserted + linesDeleted) as total
FROM table
WHERE total >= 200
GROUP BY id
This doesn't work as I'm getting an error saying:
Unknown column 'TOTAL' in where clause
I'm using RMySql for those curious.
If you sum over linesInserted + linesDeleted then there would be no problem:
SELECT id, sum(linesInserted + linesDeleted) as total
FROM table
GROUP BY id
having total >= 200
try like below
SELECT id, sum(linesInserted + linesDeleted) as total
FROM table
GROUP BY id
having sum(linesInserted + linesDeleted)>=200
Here sum is an aggregate function, so we couldn't use where to check the condition as the total is greater than or equal to 200. We should use the having to check the condition since we use sum to calculate the values.
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.
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;
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;
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