I've this MySQL database table:
+----+------------+---------+
| id | pdate | product |
+----+------------+---------+
| 1 | 2015-10-12 | BOX |
| 2 | 2015-10-12 | SHOE |
| 3 | 2015-10-12 | PEN |
| 4 | 2015-10-12 | BOX |
| 5 | 2015-10-19 | BOX |
| 6 | 2015-10-12 | SHOE |
| 7 | 2015-10-19 | SHOE |
| 8 | 2015-10-19 | PEN |
| 9 | 2015-10-19 | WATCH |
| 10 | 2015-10-26 | WATCH |
| 11 | 2015-10-26 | SHOE |
+----+------------+---------+
The column product is a dynamic column, I don't know beforehand which products are in this column. And also the pdate column will grow over time and thus I don't know the week numbers beforehand.
I want to get the following output:
+-----------------+-------+-------+
| product | WK 42 | WK 43 | WK 44 |
+-----------------+-------+-------+
| BOX | 2 | 1 | 0 |
| SHOE | 2 | 1 | 1 |
| PEN | 1 | 1 | 0 |
| WATCH | 0 | 1 | 1 |
+-----------------+-------+-------+
I want to count the number of records per week per product.
I know I need to do something with MySQL Pivot tables/queries but I'm completely stuck at the moment. How to solve this challenge?
This is not a duplicate question as the other topic is on 2 different tables, this is about a pivot query on a single table. This is a different situation.