skip to Main Content
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


  1. 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;

    $result = DB::table('table_name')->select([
        'name',
        'date',
    ])
    ->selectRaw('sum(score) AS score')
    ->groupBy(['name', 'date'])->get();
    
    return $result->groupBy('date');
    

    then you should be able to get result in a format like below;

    {
        "01-01-2023" : [
            {
                "name": "A",
                "date": "01-01-2023",
                "score": "300"
            }
        ],
        "02-01-2023" : [
            {
                "name": "A",
                "date": "02-01-2023",
                "score": "300"
            }
            {
                "name": "B",
                "date": "02-01-2023",
                "score": "200"
            }
        ],
        "03-01-2023" : [
            .
            .
            .
        ]
    }
    

    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

    $dateRange = CarbonCarbonPeriod::create(now()->startOfMonth(), now()->endOfMonth() )->toArray();
    
    $result = DB::table('table_name')->select(['name']);
    
    foreach ($dateRange as $date) {
        $dateFormat = $date->format('d-m-Y');
        $day = $date->format('j');
        $result->selectRaw("SUM(CASE WHEN Date = '$dateFormat' THEN Score ELSE 0 END) AS 'Day $day'");
    }
    
    return $result->groupBy('name')->get();
    
    Login or Signup to reply.
  2. just to keep date in group by
    ->groupBy(‘date’);

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search