34

I'm trying to refine a query that I am currently using:

SELECT `puid`,
       COUNT(DISTINCT `droid_v`) AS DROID,
       COUNT(DISTINCT `sig_v`) AS sig,
       SUM(NoExt)  AS hits
       FROM temp
       GROUP BY `puid`

And I need to get it to only count droid_v where droid_V is greater than 0. Is it possible to condition the count in this way? At the moment, it is counting zero values as countable, and I can't really change the zeros to null values.

I do not need to know the count value for droid_V = 0, I only need to count it if it has a number greater than 0. That number will always be either 0, 1, 2, 3, or 4.


I have tried:

SELECT `puid`,
   COUNT(DISTINCT CASE WHEN `droid_v` > 0 THEN 1 END) AS DROID,
   COUNT(DISTINCT `sig_v`) AS sig,
   SUM(`NoExt`)  AS hits
   FROM temp
   GROUP BY `puid`

But this gives a binary result (either 0 or 1) not the count I am expecting.

example output =

puid            DROID   sig     hits
No PUID         1       1        252
x-fmt/92        1       5       1008

anticipated output:

puid            DROID   sig     hits
No PUID         1       1        252
x-fmt/92        3       5       1008

Sample data:

id;puid;droid_v;sig_v;speed;Ext;NoExt;tally


1;"No PUID";"3";"v13";"SLOW";"0";"63";"63"
2;"x-fmt/92";"3";"v13";"SLOW";"63";"0";"63"
3;"x-fmt/92";"3";"v37";"SLOW";"63";"63";"126"
4;"x-fmt/92";"3";"v45";"SLOW";"63";"63";"126"
5;"x-fmt/92";"3";"v49";"SLOW";"63";"63";"126"
6;"x-fmt/92";"3";"v50";"SLOW";"63";"63";"126" 
7;"x-fmt/92";"5";"v13";"SLOW";"63";"0";"63"
8;"No PUID";"5";"v13";"SLOW";"0";"63";"63"
9;"x-fmt/92";"5";"v37";"SLOW";"63";"63";"126"
10;"x-fmt/92";"5";"v45";"SLOW";"63";"63";"126"
11;"x-fmt/92";"5";"v49";"SLOW";"63";"63";"126"
12;"x-fmt/92";"5";"v50";"SLOW";"63";"63";"126"
13;"No PUID";"6";"v13";"FAST";"0";"63";"63"
14;"x-fmt/92";"6";"v13";"SLOW";"63";"0";"63"
15;"No PUID";"6";"v13";"SLOW";"0";"63";"63"
16;"x-fmt/92";"6";"v13";"FAST";"63";"0";"63"
17;"x-fmt/92";"6";"v37";"SLOW";"63";"63";"126"
18;"x-fmt/92";"6";"v37";"FAST";"63";"63";"126"
19;"x-fmt/92";"6";"v45";"FAST";"63";"63";"126"
20;"x-fmt/92";"6";"v45";"SLOW";"63";"63";"126"
21;"x-fmt/92";"6";"v49";"FAST";"63";"63";"126"
22;"x-fmt/92";"6";"v49";"SLOW";"63";"63";"126"
23;"x-fmt/92";"6";"v50";"FAST";"63";"63";"126"
24;"x-fmt/92";"6";"v50";"SLOW";"63";"63";"126"
2
  • can you add a WHERE clause? Mar 5, 2012 at 1:00
  • from the example you provided, what is your expected result? Mar 5, 2012 at 2:07

5 Answers 5

49

If droid_v can only be 0, 1, 2, 3, or 4, then COUNT(DISTINCT) will never return more than 5, since there are only five possible values. Is that what you want? If so, then try this:

SELECT puid, COUNT(DISTINCT CASE WHEN droid_v > 0 THEN droid_v ELSE 0 END) - 1 AS droid /* -1 for the case where droid_v is 0 */
     , COUNT(DISTINCT sig_v) AS sig
     , SUM(NoExt)  AS hits

Update: Oops, sorry, the above is not quite right as there might not be a zero. It should be:

SELECT puid, COUNT(DISTINCT CASE WHEN droid_v > 0 THEN droid_v END) AS droid

If, on the other hand, you want a count of all the rows where droid_v > 0, then I think you want this:

SELECT puid, SUM(CASE WHEN droid_v > 0 THEN 1 ELSE 0 END) AS droid
     , COUNT(DISTINCT sig_v) AS sig
     , SUM(NoExt)  AS hits

Hope this helps.

1
  • Thats the one - I was poking away at it prior to your change, and trying to figure out what was wrong. You are correct, it migth not be Zero. Thanks for your time.
    – Jay
    Mar 5, 2012 at 2:30
19

Just use a sum

so if you want to group something check this out

select city, sum( case when gender = 'male' then 1 else 0 end ) as male,
sum( case when gender = 'female' then 1 else 0 end ) as female
from person
group by city

simple as this :D

1
  • 2
    This is the better answer. Additionally you can use MYSQL IF instead of CASE. For example, SUM( IF( gender = 'male' , 1, 0 ) ) Nov 23, 2020 at 8:19
8
SELECT
  `puid`,
   COUNT(DISTINCT CASE WHEN `droid_v` > 0 THEN `droid_v` END) AS DROID,
   COUNT(DISTINCT `sig_v`) AS sig,
   SUM(NoExt)  AS hits
2
  • Hey, thanks for the suggestion, I have edited the question to respond to your idea.
    – Jay
    Mar 5, 2012 at 1:35
  • My initial answer was wrong despite your clear question. See revised answer.
    – John Pick
    Mar 5, 2012 at 2:14
4

This is the simplest solution I can think of:

SELECT puid,
SUM(droid_v > 0) AS DROID,
COUNT(DISTINCT sig_v) AS sig,
SUM(`NoExt`)  AS hits
FROM t
GROUP BY puid

However, it is not clear if you want to count the distinct or not. If you want to count the distinct values then:

SELECT puid,
COUNT(if(droid_v > 0, droid_v, null)) AS DROID,
COUNT(DISTINCT sig_v) AS sig,
SUM(`NoExt`)  AS hits
FROM t
GROUP BY puid
1
  • Thanks for having a look - thats close, but its loses the distinct function that I need (sorry if I didn't make that clear)
    – Jay
    Mar 5, 2012 at 2:38
2

add this to the query

WHERE droid_v > 0
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.