I am trying to find the max of transactions count based on type for each user.
| id | user_id | type |
|---|---|---|
| 1 | 1 | A |
| 2 | 1 | B |
| 3 | 1 | C |
| 4 | 1 | A |
| 5 | 2 | B |
| 6 | 2 | C |
| 7 | 2 | C |
| 8 | 2 | C |
I am expecting the output to be:
| user_id | type | count |
|---|---|---|
| 1 | A | 2 |
| 2 | C | 3 |
SELECT
DISTINCT(t.user_name), t.discom, c.*
FROM transactions AS t
LEFT JOIN (
SELECT MAX(id) AS id, user_id, COUNT(id) AS `count` FROM transactions
GROUP BY user_name, discom
) AS c ON c.user_id = t.user_id
GROUP BY t.user_name
ORDER BY c.count DESC
Although, the query is running, but its taking too much time(in minutes) just to fetch the data of mere 3500 users.