0

I am trying to calculate the percentage of the data that falls into categories. I used this query:

select 
    distinct(city), 
    count(distinct id) over (partition by city) as CountofGoogle
from 
  TABLE  "
where 
    company_name = 'Google' 
order by 2 desc

I then ended up with these results:

city CountofGoogle
City A 25000
City B 6000

(obv a more extended table)

I'm at a loss as to how I create a 3rd column that will give me a % of the total? (i.e. if there was a total of 100000 then city A would be 25%. I've tried sum etc but getting errors

Thanks

Reporter
  • 3,897
  • 5
  • 33
  • 47
  • You're not the first one asking this, see for example here: https://stackoverflow.com/questions/770579/how-to-calculate-percentage-with-a-sql-statement (I know, the linked question is taged as SQLServer DB, but that's not really Db-type-specific) – Jonas Metzler Nov 23 '22 at 10:37
  • `count(distinct id) over (partition by city) * 100 / count(distinct id) over () ` – Bryan Dellinger Nov 23 '22 at 10:39

0 Answers0