how to get result count group by day in laravel - laravel

Hello Everyone can you please help me to resolve this
I want to get the user count day-wise, for example, I want to know how many users registers in last week
like date of
22-07-19
user count 20
23-07-19
user count 30
24-07-19
user count 10
25-07-19
user count 15
I want this result for last 7 day from today
basically, I want to show this in my chart please check the image here

By selecting DATE(created_at) and grouping by that, we can get the count of users that have registered each day. We can then add a simple where clause, using Carbon to help us get the lower bounds.
Example (where x = date and y = count):
User::selectRaw('DATE(created_at) as x, COUNT(*) as y')
->groupBy('x')
->where('created_at', '>', Carbon::now()->subWeek())
->get();

Related

Show only last occurence of the field

I have an issue with a report I am trying to make in SAP. The problem is that I want to only show each SR NUM only once. But there are many appearances in my report. I saw that each number has multiple activities and comments and that is why there are appearing more than once. The thing is that I only need the last appearance based on date for each SR Num and there is no filter that can help me with this. I also tried Ranking but I do not have a metric and created a new variable finding max date for each sr num. That also did not work as there are multi values.
Please help!
For example i have 3 columns sr num, date and comments. The first has 3 different nums but multiple times and the dates are all different as the comments.I need to only keep each sr num once with the most recent date and comment
I created some sample data in a free-hand SQL query which yields this...
You will need to find the maximum date for each SR Num and then only show that row for each SR Num. I used two variables to achieve this.
Var Max Activity Date...
=Max([Activity Date]) In ([SR Num])
Var Is Max Activity Date...
=If([Activity Date] = [Var Max Activity Date]; 1; 0)
Finally add a table filter to only show the rows where the Activity Date is the Max Activity Date for each SR Num.
You do not need the variables in your table in the end. I just put them there in order to visualize what is going on. That's it.
Noel

How to create a measure to count the number of times item appears in a filtered list (power BI, DAX)

Hi, I would like to create a measure in power BI to return the number of times the terminal code appears in my list. The list is filtered from a slicer when I select the service code.
Thanks for any help! been stuck at this seemingly simple problem for a few days!
If you want to count occurence in selected filter context then use ALLSELECTED:
CountOfCode = CALCULATE( COUNTROWS(Sheet1), filter(ALLSELECTED(Sheet1), Sheet1[Terminal Code ] = SELECTEDVALUE(Sheet1[Terminal Code ]) ))

Laravel, Carbon. Get count of hours for a specific month between dates

How to get the number of hours referring only to the one month between 2 dates?
For example, how to get count of hours for December for first or for second row at the screenshot?
I tried this (subMonths generated in the loop, so no worry about it):
$bookings = Booking::whereDate('departure_date', '>=', Carbon::now()->subMonths($i)->startOfMonth())
->orWhereDate('arrival_date', '<=', Carbon::now()->subMonths($i)->endOfMonth())->get();
and then:
foreach ($bookings as $book) {
echo Carbon::parse($book->departure_date.$book->departure_time)->diffInHours(Carbon::parse($book->arrival_date.$book->arrival_time));
}
But in this case I get count of hours for whole booking, how to get it only for December?
p.s. I need this for calculating the statistics (booking percentage).
You would likely need to ->addMonth() then go to the ->startOfMonth() then use diffInHours().
Carbon::parse($book->departure_date)
->addMonth()
->startOfMonth()
->diffInHours(Carbon::parse($book->arrival_date.$book->arrival_time));
I've omitted $book->departure_time otherwise the timestamp wouldn't be the first second of the first day of the month.

Query Builder summarize the field value depending on another field

I have some table
I need select an group iformation from these, but a have some conditions:
Group results by date (day). Ideally when day start on 09.00 and finsh 09.00 (24hr)
Then i need summarize values field sum where 'satus_id' = 10 into new variable example 'TotalIn' and where status_id = 12 into variable example TotalOut
Give results on view (but this no problem)
How to do it?
I write this, but i now this is wrong:
$statistic = DB::connection('mysql2')->table('payout_transactions')
->selectRaw('*, DATE(date) as day')
->where('status_id', 12)
->selectRaw('SUM(sum) AS TotalIn')
->groupBy('day')
->get();
What you could consider is:
DB::table('market')
->selectRaw('SUM(sum) as total, status_id, DATE("date") as day')
->whereIn('status_id', [10,12])
->groupBy([DB::raw(DATE('date')), 'status_id'])
->get()
That should give the sums for day, separately for both status_ids (10 or 12).

SOQL - single row per each group

I have the following SOQL query to display List of ABCs in my Page block table.
Public List<ABC__c> getABC(){
List<ABC__c> ListABC = [Select WB1__c, WB2__c, WB3__c, Number, tentative__c, Actual__c, PrepTime__c, Forecast__c from ABC__c ORDER BY WB3__c];
return ListABC;
}
As you can see in the above image, WB3 has number of records for A, B and C. But I want to display only 1 record for each WB3 group based on Actual__c. Only latest Actual__c must be displayed for each WB3 Group.
i.e., Ideally I want to display only 3 rows(one each for A,B,C) in this example.
For this, I have used GROUPBY and displayed the result using AggregateResults. Here is the result.
I got the Latest Actual Date for each WB3 as shown above. But the Tentative date is not corresponding to it. The Tentative Date is also the MAX in the list.
Here is the code I used
public List<SiteMonitoringOverview> getSPM(){
AggregateResult[] AgR = [Select WB_3__c, MAX(Tentaive_Date__c) dtTentativeDate , MAX(Actual_Date__c) LatestCDate FROM Site_progress_Monitoring__c GROUP BY WBS_3__c];
if(AgR.size()>0){
for(AggregateResult SalesList : AgR){
CustSumList.add(new SiteMonitoringOverview(String.ValueOf(SalesList.ge​t('WB_3__c')), String.valueOf(SalesList.get('dtTentativeDate')), String.valueOF(SalesList.get('LatestCDate')) ));
}
}
return CustSumList;
}
I am forced to use MAX() for tentative date. I want the corresponding Tentative date of the MAX Actual Date. Not the Max Tentative Date.
For group A, the Tentative Date of Max Actual Date is 12/09/2012. But it is displaying the MAX tentative date: 27/02/2013. It should display 12/09/2012. This is because I am using MAX(Tentative_Date__c) in my code. Every column in the SOQL query must be either GROUPED or AGGREGATED. That's weird.
How do I get the required 3 rows in this example?
Any suggestions? Any different approach (looping within in groups)? how?
Just ran into this issue myself. The solution I came up with only works if you want the oldest or newest record from each grouping. Unfortunately it probably won't work in your case. I'll still leave this here incase it does happen to help someone searching for a solution to this issue.
AggregateResult[] groupedResults = [Select Max(Id), WBS_3__c FROM Site_progress_Monitoring__c GROUP BY WBS_3__c];
Calling MAX or MIN on the Id will let you get 1 record per group condition. You can then query other information. I my case I just need 1 record from each group and didn't really care which one it was.

Resources