Assuming I have the following table:
id | category_id | item_id | created_at |
---|---|---|---|
7 | 11 | 106 | 2024-05-06 |
6 | 3 | 102 | 2024-05-06 |
5 | 11 | 101 | 2024-05-05 |
4 | 9 | 98 | 2024-05-04 |
3 | 3 | 97 | 2024-05-03 |
2 | 1 | 91 | 2024-05-02 |
1 | 11 | 89 | 2024-05-01 |
How can I select all the rows, and add a column that represents the order of the creation for each item groups by its category?
The result should be:
id | category_id | item_id | item_order_by_category | created_at |
---|---|---|---|---|
7 | 11 | 106 | 3 | 2024-05-06 |
6 | 3 | 102 | 2 | 2024-05-06 |
5 | 11 | 101 | 2 | 2024-05-05 |
4 | 9 | 98 | 1 | 2024-05-04 |
3 | 3 | 97 | 1 | 2024-05-03 |
2 | 1 | 91 | 1 | 2024-05-02 |
1 | 11 | 89 | 1 | 2024-05-01 |
Is it doable with a query with good performance? Or I will need a relationship table perhaps for that?