I have a table named Users
, with userId as one of the column, the user can be in any one of given 5 possible states which are (Unregistered, registered, subscribed, premier, unsubscribed).
I want a query which can give me a list of all those userIds which have gone through all the states.
Can someone help me out with that.
i am sharing a sample schema as well to understand the problem better
userId state created_at
1 Unregistered 1/10/22
2 Unregistered 4/10/22
3 registered 4/10/22
2 registered 5/10/22
1 registered 7/10/22
1 subscribed 12/10/22
2 subscribed 13/10/22
2 premier 22/10/22
2 unsubscribed 23/10/22
3 unsubscribed 25/10/22
1 unsubscribed 25/10/22
here, as you can see, only userId = 2 can be the correct answer, as it is going through all the required states.
3
Answers
You could count the number of unique statuses per
userid
and use that as your filter:dbfiddle here
A simple having count would do the trick.
If the
state
are not known you could use:https://dbfiddle.uk/UEnGQ53k
Otherwise if the states are known:
https://dbfiddle.uk/dBe69rP5
since you didn’t mention if there can be other possible states or duplications, you can go with count logic
instead of
groupby
, i used the windows function so that you will be able to see all the rows and the dates of each id which has all 5 states