tags:

views:

51

answers:

2

Okay so this query should be easy but I'm having a bit of difficult. Let's say I have a table called 'foo' with columns 'a', 'b'.

I'm trying to figure out the following in one query:

select how of column 'a' are available of type column 'b', this is done with the following:

mysql> select count(a),b from foo GROUP BY b;

that's straight forward. but now I want to add a third output to that query as well which shows the percentage of the result from count(a) divided by count(*). So if I have 100 rows total, and one of the GROUP BY results comes back with 20, I can get the third column to output 20%. Meaning that column a makes for 20% of the aggregate pool.

+4  A: 

Assuming you have > 0 rows in foo

SELECT count(a), b, (count(a) / (SELECT count(*) FROM foo)) * 100
FROM foo
GROUP BY b
doza
A: 

There is a risk of it running slow, best bet is to program whatever is to preform two separate queries.

SELECT count(*) INTO @c FROM foo;
SELECT count(a), b, (count(a)/@c)*100 FROM foo GROUP by b;
MindStalker