Sum of column by groupBy in Eloquent - eloquent

My database has a record for each of our engineers weekly hours. What I need to do return all the hours for a period ('period_id'), group them by engineer ('engineer_id'), and then sum each of the following cols mon, tue, wed, thur, fri, sat, and sun. Basically returning the total hours for each engineer for a period.
Hope this makes sense.
Got this far.
$totals = Hours::where('late', 0)->where('period_id', $period->id);
$hours_totals = $totals->groupBy('employee_id');

Use this:
Hours::select('engineer_id', DB::raw('sum(mon) mon'), DB::raw('sum(tues) tues'), [...])
->where('late', 0)
->where('period_id', 1)
->groupBy('engineer_id')
->get();

Related

SSRS 2008 - Why are certain months returning no results in matrix but results showing in SQL query pane?

I have searched but have been unable to find any other queries like this one. I have a query with month and year parameters and the counts are summed into a matrix. Everything works well for months Jan to Sept incl and year 2018 however when I test it with November or December 2018 and January 2019 I am getting results in the query pane but nothing in the matrix (not even zeros).
The where clause in my query is:
(edited 7/2/18 to show updated YYYY)
WHERE (DATEPART(MM, createdon) = #selectedMonth) AND (DATEPART(YYYY, createdon) = #selectedYear) OR
(DATEPART(MM, sb_provisionalstatuschangedate) = #selectedMonth) AND (DATEPART(YYYY, sb_provisionalstatuschangedate) = #selectedYear) OR
(DATEPART(MM, sb_confirmedstatuschangedate) = #selectedMonth) AND (DATEPART(YYYY, sb_confirmedstatuschangedate) = #selectedYear) OR
(sb_status = 3) AND (statecode = 1) AND (DATEPART(MM, sb_eventdate) = #selectedMonth) AND (DATEPART(YYYY, sb_eventdate) = #selectedYear) OR
(statecode = 2) AND (DATEPART(MM, actualclosedate) = #selectedMonth) AND (DATEPART(YYYY, actualclosedate) = #selectedYear)
ORDER BY sb_eventdate, BookingName
I have set up available values within the #month parameter for months 1 to 12 and within the #year parameter for 2018 to 2022 (inclusive).
An example of one of the expressions (first column):
=Sum(iiF(DatePart("m", Fields!createdon.Value) = Parameters!selectedMonth.Value And DatePart("yyyy", Fields!createdon.Value) = Parameters!selectedYear.Value, 1, 0))
I tested everything for user selection of Jan 2018 and exported the results and counted what the results should be and everything was correct.
As a lot of my testing had been on the Jan 18 data I also tested all the other months in 2018. Everything went well until I got to November and December when I get no results - not even zeros.
This would normally indicate a problem with the data and therefore no results so I ran the query in SQL query pane (inserting a 12 where query shows #Month and 2018 where query shows #year, which I exported and checked - (the top 10 results showing should all be counting in the matrix under 'cancelled'). There are results for all of the columns. If there had been no results I would still have expected to see zeros.
Is anyone able to explain why I am getting nothing for Novemebr, December 18 and also for Jan 19?
Try this: in your where clause "DATEPART(YY" amend this to "DATEPART(YYYY"
on the 4th line of your query you have :
(DATEPART(MM, sb_eventdate) = 2018)
This should be:
(DATEPART(YYYY, sb_eventdate) = 2018)
I have managed to sort out the problem and it was that another instance of the database had been created which has now become the main database and the one that used to be the main database is now for emergency only - obviously does not hold the same data. I was querying one instance on the SQL query pane and another instance in visual studio. I apologise for wasting your time on something that would not have happened if our communications were better.

Dateparse MON in Tableau

I have a data source that returns a date as a string in the form of 'MON YYYY' (APR 2014, MAY 2014, etc.).
I tried making a calculated field off of this information with the following formula:
DATEPARSE('MMM YYYY', [Field1])
This is a sample set of the data I'm getting (I added the pipe as a divider):
Field1 || Calculated Field
APR 2014 || 12/22/2013
APR 2015 || 12/28/2014
APR 2016 || 12/27/2015
AUG 2014 || 12/22/2013
AUG 2015 || 12/28/2014
AUG 2016 || 12/27/2015
I've also tried to add a day field, but that results in the same incorrect data as above:
DATE(DATEPARSE('dd MMM YYYY','01 ' +[Field1]))
Is there something I'm perhaps misunderstanding about the dateparse function?
It turns out that YYYY means something totally different than yyyy. The capitalized MMMwas necessary for the MON type description. This worked for me:
DATE(DATEPARSE('MMM yyyy',[Field1]))
If you date the date off you'll get the hour, minute, second fields as well.
Dateparse converted it from a string [Field1] into a Date type using the aforementioned format of three digit month, a space, and a four digit year (e.g. AUG 2014 -> 8/2/2014).

Difference between two isodates in mongo

If I have two ISODates such as:
Tue Sep 18 1984 00:00:00 GMT+0100 (CET)
and
Sat Jun 21 2014 10:00:00 GMT+0100 (CET)
how do I get a difference between them using the mongo console? Specifically the difference in years?
they are from different collections so I can't use an aggregation for this.. :(
ISODate() is just a convenient wrapper around a standard JavaScript Date object so you can use the standard Date methods or calculate the difference yourself (date values are stored in milliseconds):
> var date1 = ISODate("1984-09-18");
> var date2 = ISODate("2014-06-21");
> date2.getFullYear() - date1.getFullYear()
30
> var yearMS = 365 * 24 * 60 * 60 * 1000; // a year in milliseconds
> parseFloat((date2-date1)/yearMS).toFixed(2)
29.78

IndexedDB with range queries

I would like to find which date ranges overlaps another date range in IndexedDB.
Something like the following query:
SELECT * FROM events
WHERE (periodStart >= start AND periodStart < end)
OR (start >= periodStart AND start <= periodEnd)
Events looks like the following object:
[{
title: 'foo',
start: 'Tue Oct 29 2013 10:19:52 GMT-0400 (EDT)',
end: 'Tue Oct 29 2013 13:19:52 GMT-0400 (EDT)'
},
{
title: 'bar',
start: 'Tue Oct 30 2013 00:00:00 GMT-0400 (EDT)',
end: 'Tue Oct 31 2013 00:00:00 GMT-0400 (EDT)'
}]
To my knowledge, no ability to do unions in indexeddb (the OR).
So, as that kinda sucks, here is a half thought out solution that maybe puts you on the course: something like find the min of the set, and the max of the set, and then a query descending and a query ascending but just get the first record from each cursor, and then, something.

Checking if Date is between a range of dates - Logic Issue - BEGINNER

I have a Person object. and this person object has the following attributes;
Name
StartDate
EndDate
I am saving this Person objects to an array. This array might contain more than 100 person objects.All of the above attributes are Strings.
The following is an example of person objects in that array;
John, Tue Feb 22, Thr Mar 30
Jack, Wed Mar 09, Fri Apr 21
Jack, Thu Mar 19, Fri Dec 20
Jack, Tue Jan 08, Fri Apr 26 etc..
Now i need to will supply a date, say for example Wed 29 Mar, and i need to check if it's in the range of StartDate and EndDate in the array of persons object. I have a pseudo-code for the scenario.
The date i am going to check against the StartDate and EndDate are Strings too.
Pseudo-code:
if Startdate >= providedDate && EndDate <= providedDate {
// Add to an Array
else
//Do not add to an array
Since, the StartData, EndDate and ProvidedDate are all Strings how can i check if its Greater than or lesser than the provided date ?
Note: I need an approach that doesn't use NSPredicate
In order to compare values you need values that are comparable. You could use NSDate objects, you could cast dates into sortable character/numeric form (eg 20120425), or you could use custom objects that implement their own compare:options: methods.
Or you could write a separate compareString:string1 toString:string2 method and use the strings as you have them.
You just have to make up your mind which you want to do.
What are you being tested on here? It'd odd to have a date stored as a string. Are you meant to parse the string dates in order to compare them to the provided date? If so, I'll stay away from any kinds of date classes.
If so, I'd just split it into 3 strings -- day of the week, month, day. You don't care about day of the week.
You don't have a year so you can't do any logic about that... I guess just assume it's all in the same year. (You could do something crazy like calculate 'which recent year did this day fall on this day of the week', but come on...)
Associate each month with an integer. Let's say, using a hash map. As in Months.put('January',1), etc.
Get the number out of each date -- just convert from a string to an integer.
Then the logic for whether something is before or after a date is (if -1 is myMonth is before, 0 is they're the same, and 1 is myMonth is after)
if (Months.get(myMonth) < Months.get(providedMonth)) return -1
else if (Months.get(myMonth) > Months.get(providedMonth)) return 1
else
if (myDate < providedDate) return -1;
else if (myDate > providedDate) return 1;
else return 0;