I could really use some help with the problem I'm facing. I'm trying to dynamically add columns to a query based on the number of times a user_id occurs in this example event_table.
So, this is the example event_table. Every time the user_id comes up, I want another column with CONCAT("event_", times_occured) added to the result.
id | user_id | create_date |
---|---|---|
1 | 344 | 2021-05-25 |
2 | 25 | 2021-05-25 |
3 | 344 | 2021-07-06 |
4 | 344 | 2021-07-07 |
5 | 3245 | 2021-08-25 |
6 | 52 | 2021-09-14 |
7 | 52 | 2021-10-11 |
The query result should be formed this way.
user_id | event_1 | event_2 | event_3 |
---|---|---|---|
25 | 2021-05-25 | null | null |
52 | 2021-09-14 | 2021-10-11 | null |
344 | 2021-05-25 | 2021-07-06 | 2021-07-07 |
3245 | 2021-08-25 | null | null |
I'm not sure if this is possible, and if it is, do I need to use recursion or loops?