I have a very simple table that contains Status_var and description. You can see example here:
http://sqlfiddle.com/#!18/3e0d1/5
I need to be able to count how many ‘Pass’ string occurences are in Status_var column and calculate percentage. Example table below:
In the above example, we have 2 "Pass" and 1 "Fail" occurences. The result should be 66%.
I have found a very simmillar solution to this problem in this post:
https://dba.stackexchange.com/questions/311416/mysql-getting-the-percentage-of-an-occurrence
And I tried to implement it in sqlfiddle:
http://sqlfiddle.com/#!18/3e0d1/22
3
Answers
Pretty simple approach is adding another
SELECT
around your query specifying the appropriate condition (you’ll have to give the calculated column a name for); this might look like:EDIT: Your second query you adopted from the other answer actually seems to be fine – on mySQL! See sqlfiddle – but you need to set mySQL as database, you tried on MS SQL which doesn’t seem to accept comparisons at this place; it seems to interpret the equal-sign as assignment, as the query
reveals…
Here is my solution for your problem:
For your mentioned link i would go with:
To get the result 66.
This could be more efficient, since will count only the ones with
Status_var = 'Pass'
:Also an index On
Status_var
will be helpfullDemo here