Name | Date | Score |
---|---|---|
A | 01-01-2023 | 100 |
A | 01-01-2023 | 200 |
A | 03-01-2023 | 300 |
B | 02-01-2023 | 400 |
B | 03-01-2023 | 100 |
B | 03-01-2023 | 100 |
i have this table and i want to seperate it into multiple column of date and SUM the score on that date using Query Builder laravel or Raw SQL so it become like :
Name | Day 1 | Day 2 | Day 3 |
---|---|---|---|
A | 300 | 0 | 300 |
B | 0 | 400 | 200 |
all of this is upto the current month so january until 31 and so on
2
Answers
You aren’t providing anything like your attempted query, how you are passing the date ( it is a range, month only etc ), and your desired json ouput.
its hard to even assume how you are going to do things specially you are passing a column value as column name in your desired result (which doesn’t make much sense with raw sql query unless those columns
aren’t dynamic).
but to give you a starting point, you can simply group them by name, then date, then do another grouping by date in the collection
e.i;
then you should be able to get result in a format like below;
For you desired table result, thats better be changed to a dynamic rows instead of dynamic column
EDIT
In reference with Karl answer, you can loop through a date range and inject additional select statement.
e.i. current month dates
just to keep date in group by
->groupBy(‘date’);