Username79123 Username79123 - 7 months ago 22
SQL Question

Percentage of requests SQL

So, I have this table:

Table1
|Number | abc |
|100 | Yes |
|200 | Yes |
|300 | Yes |
|400 | No |
|500 | No |


What I want is the percentage of values that is "yes". In this case, the desired OUTPUT is
60%


I thought that by dividing the number of "yes" by the total number It would do it, but i can't "join" all things.

I know that the number of "yes" is

select count(abc)
from table1
where abc='yes'


And the total number is

select count(*)
from table1


How do i get the desired output?

Answer

A query that works in all SQL engines is

select sum(case when abc = 'yes' then 1 else 0 end) * 100 / count(*)
from your_table
Comments