Similar question to this however I want to count how many different distinct values appear for one user_id. If I had the following table:
USER_ID DATA
123 abc
234 abc
123 def
456 def
123 abc
I would want the following results:
USER_ID COUNT
123 2
234 1
456 1
Because '123' has both 'abc' and 'def' data values, where as '234' and '456' only have the one distinct data value.
Currently I have the following pseudo-code:
rows = SELECT DISTINCT user_id FROM table
for for in rows:
result = SELECT DISTINCT user_id, data FROM table WHERE user_id = <row>
if result.rowcount > 2:
doSomething()
But this takes a long time as it has to make a call for each user_id.
Can this be done in one MySQL command?