How to get daily record and sum up [duplicate] - php

This question already has answers here:
Laravel Group By Date
(2 answers)
Closed 2 years ago.
How to get the daily record and sum them up, plus group it by date? Please see my code below.
$sales = DB::table("orders")
->where('status', 8)
->select(
'grand_total',
'order_date',
DB::raw("SUM(grand_total) as total_sales")
)
->groupBy('order_date')
->get();
dd($sales);
It returns the right dates, but wrong sales. How can I sum sales each day?

Okay I did it by adding DB::Raw('DATE(ord_date) day') inside the select. I referenced this topic and I did a little bit work around.
Reference:
Laravel Group By Date
The problem is it also groupBy the time, which is wrong because I want to get the sales daily.

Related

Laravel: DATE_ADD in where clause [duplicate]

This question already has answers here:
Is there a way to subtract an amount of days from a date in SQL?
(4 answers)
Closed 7 months ago.
I want to subtract days from the db column and then compare it with the current date using DATE_ADD function. Is this possible?
$valid_status_query->whereRaw('DATE_ADD("d",-3, "date_to")', '<=', Carbon::now()->format('Y-m-d'));
For example, if the column date is 2022-07-20 then it should use 2022-07-17 to compare in where clause at the end.
Use it like this:
$valid_status_query->whereRaw("DATE_SUB(date_to, INTERVAL 3 DAY) <= ?", [Carbon::now()->toDateString()]);

PHP Calendar, sum weekly hours and echo one by one [duplicate]

This question already has an answer here:
Sum hours from time tracker to calendar by day
(1 answer)
Closed 4 years ago.
I need help on how to group my QUERY output by week.
The current output looks like this:
What I want:
I'd like it to sum all the columns that contain the same week number and hours into one column.
My current QUERY:
$query = "
SELECT userhours_id, user_hours, hours_timeoccur, SUM(user_hours) as 'myhours' FROM hours h
where h.userhours_id = $loginuser_id
AND h.hours_timeoccur = '$datetime'
";
I did try add the following to group it by $datetime which is each days number in 20xx-xx-xx format but it didn't make any difference:
GROUP BY WEEK(WEEKOFYEAR(h.hours_timeoccur))
I also tried to add an PHP IF statement which said if $weeknum (which is the week of each date), but I never really figured out what parameters I would be use.. so got stuck in this:
if ($weeknum = $weeknum ) {
'myoutput'
}
Any idéa on how to accomplish this kind of request? Thanks in advance!
This query should give the data grouped by user and year week:
SELECT userhours_id, WEEKOFYEAR(h.hours_timeoccur) AS hours_week, SUM(user_hours) as 'myhours'
FROM hours h
WHERE h.userhours_id = $loginuser_id
GROUP BY userhours_id, hours_week
I removed the date where clause as if you only select for one date then it would not show useful information, maybe it would be best to provide a date range and query BETWEEN?

How to select from database using laravel where clause and created_at

I want to select all the data on a database using laravel db query or Eloquent ORM. So i want all the data that are other than 3 days from the day they where created. Data 'A' is created on 15th i come in on 16th to check, i won't see Data 'A' but i may see others older than 3 days or equal to 3 days. If i come back on the 18th or 19th i should see Data 'A', obviously because it now older than 3 days or equal to.
so i wrote this code that is not working below
$users = DB::table('matched_users')->where( 'created_at', '>=',
Carbon::now()->subDays(3));
so can anyone correct this for me.
when i did that it work but it shows both someone that his date is just one day one. so is that not enough code
Data Name - created_at
ozil 21/04/2012 16:09:22
mark 21/04/2012 16:09:22
cyril 22/04/2012 16:19:21
so today is 25/04/2012 if run the query of run the above query as it is i get all the result back.
But if change the >= to <= the result is an empty collection
I guess you mean 'older than 3 days from the day they where created'. In this case the query should look like this:
DB::table('matched_users')->where('created_at', '<=', Carbon::now()->subDays(3));
$users = DB::table('matched_users')->whereDate( 'created_at', '<=', Carbon::now()->subDays(3))->get();
is the right way to write the code

Get Date time differnce base result (laravel 5.3)

I have a deliverables table and i want to get results where difference between current date time is 2 days from created date.
e.g today is 4-19-2017 i want to get all records where deadline is 4-21-2017 because here is difference of 2 days between 4-19-2017 and 4-21-2017 or also if 4-20-2017 it should also retrieve how ever if difference is more than 2 it should not be retrieve.
In short if Difference between current date and deadline is 2 days or less should be retrieve
I tried as
$deliverables_dead = DB::table('deliverables')->where('user_id' , Auth::user()->id)->where('deadline' , '>' , Carbon\Carbon::now())->get();
its get all records where is deadline greater than current time.
Please help how can i get answer of above bold statement......
Use whereBetween() and Carbon's addDays():
DB::table('deliverables')
->where('user_id', auth()->id())
->whereBetween('deadline' , [Carbon\Carbon::now(), Carbon\Carbon::now()->addDays(2)])
->get();

Get Last n Months Data Group by Week - Laravel

I am trying to get last 4 months data filtered by week in Laravel. This is code I currently have:
$analytics =
Data::select([
DB::raw('WEEK(p_date_time) AS week'),
DB::raw('COUNT(id) AS count'),
])
->where('p_id', $p_id)
->where('p_date_time', '>=', Carbon::now()->subMonth(4))
->groupBy('week')
->orderBy('week')
->get()
->toArray();
This code gives me 52 results based on Data for 1 full year (As I am using WEEK). But I need around 17 results for the last 4 months.
I can make that happen by gathering of each day and then adding 7 days data as chunk for last 4 months, but how can I do it with query alone?
Thanks.
Maybe just a typo in your Carbon query. Change subMonth(4) to subMonths(4). With 's'.

Categories