Is there a nice way in MySQL (older version) to replicate the SQL Server function ROW_NUMBER()?
I am trying to find 2 most used categories with their usage count per day using this query but ROW_NUMBER and PARTITION are not available in my old version of SQL.
Select a.*
FROM
( SELECT
dDay,
category,
COUNT(*) as CountOfCategory,
ROW_NUMBER() OVER (PARTITION BY dDay ORDER BY COUNT(*) DESC) AS intRow
FROM Table1
GROUP BY category, dDate ) as a
WHERE intRow <= 2
ORDER BY dDay,
intRow;
There is a variation of this question here but COUNT(*)
in my query does not seem to fit the solutions there.
Input
dDay | Category |
---|---|
1 | car |
2 | bike |
2 | car |
1 | car |
3 | truck |
1 | bike |
1 | car |
3 | car |
3 | car |
2 | bike |
1 | bike |
2 | truck |
2 | truck |
2 | truck |
Expected Output: Top 2 categories (with their total count) per day
dDay | Category | CountOfCategory |
---|---|---|
1 | car | 3 |
1 | bike | 2 |
2 | bike | 2 |
2 | truck | 2 |
3 | car | 2 |
3 | truck | 1 |