Further to this question: MySQL – Conditional COUNT with GROUP BY
Starting from the MySQL:
SELECT puid,
COUNT(DISTINCT CASE WHEN droid_v > 0 THEN droid_v END) AS droid,
COUNT(DISTINCT sig_v) AS sig,
SUM(`Ext`) AS hits
FROM temp
GROUP BY puid
Is possible introduce a condition that will only give a result where SUM(``Ext``) AS hits is greater than 0?
Based on the query above, my dataset returns occasions where the SUM value is 0 – this is because I am not checking the contents of NoExt prior to summing it.
I think I should be adding an AND into the Count statement, to check that NoExt is > 0, I can’t see where this fits.
I have tried a few permutations of COUNT(DISTINCT CASE WHEN droid_v > 0 THEN droid_v END)AND Ext>0 AS droid, but I am very much out of my syntactical depth at this point…
I have a feeling I might have to do this on the python side of my tool, but it would be good to know if this can be achieved on the MySQL side.
Just add:
To the end of the query after the GROUP BY.
HAVING is kind of like WHERE but operates on the groups specified by GROUP BY, allowing you to only include the groups you want.