Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ajvanho's avatar
Level 14

last 30 days...

How to make query that show ALL last 30 days with totalpirce. If i dont have created_at certain date, to show me 0 for that day?

$orders = DB::select('SELECT DATE(created_at) as stat_day, SUM(billing_totalprice) from Orders GROUP BY DATE(created_at) ORDER BY stat_day;');

0 likes
14 replies
bobbybouwmann's avatar

You can probably do this

use Carbon\Carbon;

$dateFrom = Carbon::now()->subDays(30);
$dateTo = Carbon::now();

$total = DB::table('orders')
    ->whereBetween('created_at', [$dateFrom, $dateTo])
    ->groupBy('created_at')
    ->sum('billing_totalprice');

// Or using the Order model

$total = Order::whereBetween('created_at', [$dateFrom, $dateTo])
    ->groupBy('created_at')
    ->sum('billing_totalprice');

Ajvanho's avatar
Level 14

Nope :( both method give me $total = 186 !?! Not array, not collection

bobbybouwmann's avatar

Aah, you want to have all results grouped by date including the number. Try this

$orders = DB::table('orders')
    ->select('created_at', DB::raw('SUM(billing_totalprice) AS total_price'))
    ->whereBetween('created_at', [$dateFrom, $dateTo])
    ->groupBy(DB::raw('DATE(created_at)')
    ->get();
Ajvanho's avatar
Level 14

Nope :( I have this array: I have sum for first 2, beacuse it have same time, anyway I want to show me array of all 30 days, and for the days where I dont hvae order to show me total price: 0.

[
{
"created_at": "2020-03-16 00:00:00",
"total_price": "186"
},
{
"created_at": "2020-03-18 09:07:25",
"total_price": "147"
},
{
"created_at": "2020-03-18 10:00:00",
"total_price": "50"
}
]
Ajvanho's avatar
Level 14

syntax error, unexpected ';', expecting ')'

I try to find where it is, but something is not ok with updated reply

Ajvanho's avatar
Level 14

I fixed with )

$orders = DB::table('orders')
    ->select('created_at', DB::raw('SUM(billing_totalprice) AS total_price'))
    ->whereBetween('created_at', [$dateFrom, $dateTo])
    ->groupBy(DB::raw('DATE(created_at)'))
    ->get();

Now I have error: 
SQLSTATE[42000]: Syntax error or access violation: 1055 '.orders.created_at' isn't in GROUP BY (SQL: select `created_at`, SUM(billing_totalprice) AS total_price from `orders` where `created_at` between 2020-03-06 12:08:29 and 2020-04-05 12:08:29 group by DATE(created_at))
Ajvanho's avatar
Level 14

I fixed, I dont have errors, but I dont have result that i want, to show me all 30 days array or collection..

$orders = DB::table('orders')
    ->select(DB::raw('DATE(created_at)'), DB::raw('SUM(billing_totalprice) AS total_price'))
    ->whereBetween('created_at', [$dateFrom, $dateTo])
    ->groupBy(DB::raw('DATE(created_at)'))
    ->get();
bobbybouwmann's avatar

Jep that should do it. I forgot to add the groupBy value to the select statement. Sorry about that.

1 like
Ajvanho's avatar
Level 14

Yup, but dont do it what I want :)

NicolasMica's avatar

👋 Hi there !

I think the last query you used is fine but I'm guessing that your database may not contain a record for each day, meaning you'll get less than 30 days.

You should build that result after the request, it will be much easier than trying to do that within a SQL query. You could use the CarbonPeriod API to loop on each day and populate the date with the matching record.

Ajvanho's avatar
Level 14

Thanks for the suggestion, but I wouldn't know how to do it. I'll try to click something for me. I was wondering if it could be done with the SQL function. I guessed that I should looped on. I've been trying to do some solutions already, but I'm not succeeding.

Ajvanho's avatar
Level 14

I made last 30 days array, but I dont know next step...

$list = Carbon::now()->subDays(30);
for ($i = 0 ; $i <= 30; $i++) {
    $dates[] = $start->copy()->addDays($i)->format('Y-m-d');
     
}
Ajvanho's avatar
Level 14

One of the problem is that column name is DATE(created_at). I try to change AS, but I have error.

Please or to participate in this conversation.