This will tab the user_id into the stream.
SELECT thread_id, user_id, COUNT(*) as postings FROM thread_posts GROUP BY thread_id, user_id
But you only want to select the top user for each thread
SELECT thread_id, user_id, postings FROM ( SELECT thread_id, user_id, COUNT(*) as postings FROM thread_posts GROUP BY thread_id, user_id ) HAVING postings = max(postings)
which is equivalent
SELECT thread_id, user_id, COUNT(*) as postings FROM thread_posts GROUP BY thread_id, user_id HAVING postings = max(postings)
The HAVING keyword is typically used with the aggregate operation for cherries β select aggregated output rows that match the conditions in the HAVING clause.
The HAVING clause is different from the WHERE clause, in which the HAVING clause filters the result of the query. Whereas the WHERE clause filters the request input. Since the HAVING clause filters the result of the query, it should appear after the ORDER BY and GROUP BY clauses.
source share