Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Average on a count() in same query

Tags:

I'm currently working on an assignment which requires me to find the average on the number of resources for each module. The current table looks like this:

ResourceID   ModulID    1            1    2            7    3            2    4            4    5            1    6            1 

So basically, I'm trying to figure out how to get the average number of resources. The only relevant test data here is for module 1, which has 3 different resources connected to it. But I need to display all of the results.

This is my code:

select avg(a.ress) GjSnitt, modulID from  (select count(ressursID) as ress   from ressursertiloppgave  group by modulID) as a, ressursertiloppgave r group by modulID; 

Obviously it isn't working, but I'm currently at loss on what to change at this point. I would really appreciate any input you guys have.

like image 416
Coss Avatar asked Oct 25 '11 17:10

Coss


People also ask

Can you take the average of a count in SQL?

How to use the AVG function in SQL. The AVG function finds the arithmetic mean for a group of records in a SQL table. An average, or arithmetic mean, is the sum of a group of numbers divided by the count for that group. For example, 2+4+4+6+6+8 is 30 divided 6 which results in an average of 5.

How do you find the average of a count?

How to Calculate Average. The average of a set of numbers is simply the sum of the numbers divided by the total number of values in the set. For example, suppose we want the average of 24 , 55 , 17 , 87 and 100 . Simply find the sum of the numbers: 24 + 55 + 17 + 87 + 100 = 283 and divide by 5 to get 56.6 .

How do you use sum and count together in SQL?

SUM() and COUNT() functionsSUM of values of a field or column of a SQL table, generated using SQL SUM() function can be stored in a variable or temporary column referred as alias. The same approach can be used with SQL COUNT() function too.

How do I find the average of each row in SQL?

The average is simply calculated by summing the amounts and divide by the difference between min and max dates for each row.


1 Answers

This is the query you are executing, written in a slightly less obtuse syntax.

SELECT   avg(a.ress) as GjSnitt   , modulID FROM   (SELECT COUNT(ressursID) as ress     FROM ressursertiloppgave    GROUP BY modulID) as a CROSS JOIN ressursertiloppgave r    <--- Cross join are very very rare! GROUP BY modulID; 

You are cross joining the table, making (6x6=) 36 rows in total and condensing this down to 4, but because the total count is 36, the outcome is wrong.
This is why you should never use implicit joins.

Rewrite the query to:

SELECT AVG(a.rcount) FROM    (select count(*) as rcount     FROM ressursertiloppgave r    GROUP BY r.ModulID) a 

If you want the individual rowcount and the average at the bottom do:

SELECT r1.ModulID, count(*) as rcount FROM ressursertiloppgave r1 GROUP BY r1.ModulID  UNION ALL    SELECT 'avg = ', AVG(a.rcount) FROM    (select count(*) as rcount     FROM ressursertiloppgave r2    GROUP BY r2.ModulID) a 
like image 75
Johan Avatar answered Sep 28 '22 07:09

Johan