user | enabled_app | app_id |
---|---|---|
[email protected] | active | 2553 |
[email protected] | inactive | 2553 |
[email protected] | waiting | 2553 |
[email protected] | active | 2554 |
[email protected] | active | 2555 |
[email protected] | waiting | 2555 |
[email protected] | active | 2556 |
I have a above table, and expected output would be
count | |
---|---|
[email protected] | 1 |
[email protected] | 2 |
Let me explain we are displaying each user has how many enabled app which are active state.
So lets take the first email id [email protected] which returns 1 because this user has 2 app_id associated with it. 2553 and 2554. now in 2553 we wont consider because although it has active it also has an inactive value in it. But for app_id 2554 we have an active enabled_app but there is no inactive value for it. Hence [email protected] has count as 1
similarly [email protected] has 2 active apps of different app_id.
3
Answers
I would use a NOT EXISTS condition:
Using
EXCEPT
(aka minus) you get a raw data of the active applications that are not in inactive stateAs a positive effect the set operation removes duplicates, so you need not to thing about if
count (distinct ...
should be used.The next step is a simple aggregation