I appreciate this is a question that has been asked a few times, I have looked at the answers I can find, and can’t seem to find a solution that fits my specific dataset… (mostly likely due to my newbies level, and not really fully understanding the syntax implications for ordering the various elements for the queries).
I’m trying to run two ‘counts’ into a single resulting table – I can get the individual tables just fine – be being able to aggregate the results would hugely speed up my analysis..
The specific queries are:
(1)
SELECT `PUID`,`DROID_V`,`SIG_V`,`SPEED`
, COUNT(distinct NAME) as NoExt
FROM sourcelist, main_small
WHERE sourcelist.SourcePUID = 'MyVariableHere'
AND main_small.NAME = sourcelist.SourceFileName
AND sourcelist.hasExtension = 0
GROUP BY `PUID`,`DROID_V`,`SIG_V`,`SPEED`
ORDER BY `DROID_V` ASC, `SIG_V` ASC, `SPEED`;
and (2)
SELECT `PUID`,`DROID_V`,`SIG_V`,`SPEED`
, COUNT(distinct NAME) as Ext
FROM sourcelist, main_small
WHERE sourcelist.SourcePUID = 'MyVariableHere'
AND main_small.NAME = sourcelist.SourceFileName
AND sourcelist.hasExtension = 1
GROUP BY `PUID`,`DROID_V`,`SIG_V`,`SPEED`
ORDER BY `DROID_V` ASC, `SIG_V` ASC, `SPEED`;
If what I want to do is possible, I’d really like to see if I can impliment a 3rd count:
(3)
SELECT `PUID`,`DROID_V`,`SIG_V`,`SPEED`
, COUNT(distinct NAME) as All
FROM sourcelist, main_small
WHERE sourcelist.SourcePUID = 'MyVariableHere'
AND main_small.NAME = sourcelist.SourceFileName
GROUP BY `PUID`,`DROID_V`,`SIG_V`,`SPEED`
ORDER BY `DROID_V` ASC, `SIG_V` ASC, `SPEED`;
I wonder if someone might be able to unpick what I have done, and let me know if what I want is possible – there is some common elements for the 3 counts, (the tables, the group conditions and the order details) and the difference being the WHERE conditions.
Even with the two count query, there would be null values, where count A would result in different sets than count B – so I wondered if the counts would need to result in the same basic shape (i.e. same row builds, just different count values, rather than count specific row builds resulting in a null return for one of the counts)
To combine (1) & (2) you can use a
COUNT(distinct IF( .., .., ..))construct and then remove the WHERE condition onhasExtension, allowing you to combine (3) in too: