简体   繁体   中英

SQL find percentage of group?

I have an SQL table with a column of names and a column of prices they paid for tickets. What query can I use to find the percentage of people who paid more than £100? Thanks

You can use avg() to get the ratio:

select avg(case when price > 100 then 1.0 else 0 end)
from t;
select 
   100.0                                      -- to get a percentage
   * count(case when price > 100 then 1 end)  -- who paid over 100?
   / count(*)                                 -- all rows
from tickets

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM