I have a rather advanced query I need to do on a table that holds trouble tickets sent in by customers, to gather statistics about the tickets, and to order and sort by various counts and/or column values.
Tickets table:
+---------------------------+
| product_id | issue |
+------------|--------------+
| 1 | missing-part |
| 1 | missing-part |
| 1 | broken-part |
| 2 | broken-part |
| 2 | broken-part |
| 2 | missing-part |
| 2 | missing-part |
| 2 | missing-part |
+---------------------------+
First, I need to count the total number of tickets per product. In this case, product_id 1 has 3 tickets and product_id 2 has 5 tickets. Then, I need to get the values of the issue column and the number of occurrences of each. So at this point, I'm expecting something like this:
[
'product_id' => 1,
'issues' => 3
'issue_nums' => [
'missing-part' => 2,
'broken-part' => 1
]
],
[
'product_id' => 2,
'issues' => 5,
'issue_nums' => [
'missing-part' => 3,
'broken-part' => 2
]
]
Then, I need to be able to order by the number of of occurrences of each issue, for example: ORDER BY COUNT(missing-part) or COUNT(broken-part). In other cases, I just need to be able to order the products by number of issues in total rather than by the issue values themselves (so ordering by COUNT(issues)).
Thanks!