I have this query to get the percentages of different values in a specific column:
select dma, count(*) / const.cnt *100 as my_percent
from full_db3 cross join
(select count(*) as cnt from full_db3) const
group by dma
order by my_percent desc limit 10;
Step one: I need to amend this so it gives me the top 10 values in the column dma
based on the value of another column. For example, if the value in column gender
is "f", what are the top 10 values in column dma
. Something like this (which doesn't work, of course).
select dma, count(*) where gender = 'female' / const.cnt *100 as my_percent
from full_db3 cross join
(select count(*) as cnt from full_db3) const
group by dma
order by my_percent desc limit 10;
The above throws an error, but should get the point across.
Step 2 would be looping through all my columns and returning the top 10 values in every column (not just dma
) give the value of a specific column (e.g., gender
= 'female'). What's the best way to do this?