I spent a lot of time looking for this, please let me know if duplicating.
I need to write a grouped query that returns categories of records counting each category type. Something like that:
select categorynum, count(*) from tbl group by categorynum;
So far so good. Now I need to determine how many percent of the total amount of each category takes. The best I came up with is that I don't like it, it feels dirty:
select categorynum, count(*), count(*)/(select count(*) from tbl) from tbl group by categorynum;
It works, but it really pushes me to do it that way. The database I use is compatible with Postgres, and count(*) in the table is very fast, so there arenβt a lot of hits to do count(*) in the table, although I would like to write better SQL, if at all possible.
So is there a better way to write this? This is a situation that I often encounter, so I would like to write my queries correctly.
source share