Garrett Smallwood
Garrett Smallwood

Reputation: 389

MySQL - Sum if, but only if all conditions meet

I have a table with multiple days of the week. Each day of the week has unique attributes, such as if ice cream was successfully delivered on that day:

ID       DAY_WEEK      ICE_CREAM 
 1        Monday           1
 2        Monday           0
 3        Monday           1
 4        Monday           1
 5        Tuesday          0
 6        Tuesday          0
 7        Wednesday        0
 8        Wednesday        1
 9        Wednesday        0
 10       Thursday         1
 11       Thursday         1

Currently I'm using a count for each DISTINCT id in order to assemble the number of successful days where ice_cream = 1 or = 0 but it counts regardless if all days were successful. How do I only count day of the week if all of the days result in a successful/non-successful ice-cream run; such as Thursday or Tuesday?

Upvotes: 2

Views: 716

Answers (1)

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

SELECT DAY_WEEK
FROM table
GROUP BY DAY_WEEK
HAVING COUNT(DISTINCT ICE_CREAM) = 1

You can see the result on SQL Fiddle kindly given by JW

Upvotes: 3

Related Questions