Reputation: 23
Hello i have a Database that looks like that
------------------
| Tag | Value |
------------------
| A | Passed |
| A | Failed |
| B | Failed |
| A | Failed |
| B | Failed |
------------------
I am trying to write a query to get the percentage of passed per type
output example:
A: 33%
B: 50%
so far i tried multiple queries but the closest result i had was:
SELECT Tag, ((SELECT COUNT(*) FROM TABLE WHERE value= 'Passed') / Count(*)) * 100 AS 'Percentage to all Passed' FROM Table WHERE Value= 'Passed' GROUP BY Tag;
but the result i got where not correct
Any help on this ?
Upvotes: 1
Views: 29
Reputation: 164099
You can do it with the aggregate function AVG()
:
SELECT Tag,
100.0 * AVG(value = 'Passed') AS `Percentage to all Passed`
FROM Table
GROUP BY Tag;
Upvotes: 1
Reputation: 133370
You could use a cross join
SELECT Tag
, count(*)/t.tot
FROM my_table m
cross join (
select count(*) tot
from my_table
) t
where m.value ='passed'
Upvotes: 1