Querying a List of objects to find the the day of the week with the most items - linq

There's got to be a better way!
I have a bunch of log records stored in a List. Each log record has a CreateDate field and what I'm trying to do is find an efficient way to query the List object to find the day of the week with the most log records (Monday, Tuesday, etc...).
I'm thinking that this can be done using Linq but I don't know Linq well enough. It may be that I'll have to do this the long way by getting a count for each specific day and then comparing them against each other to find the day with the most logs but I'm hoping that someone will be able to show me a cool way to do it.
Thanks.

Loglist.GroupBy(log => log.CreateDate)
.Select(list => new { DayOfWeek = list.Key.DayOfWeek, Count = list.Count()})
.OrderByDescending(list=>list.Count);
This will return a list of (DayOfWeek, Count) in Descending Order. If you need only the largest count dayofweek, apply .First() at the end of the above list.

Related

Sum on Count in hql - error Not yet supported place for UDAF 'count'

I'm new here so please be gentle, my first question after using this website for a long time regards the below:
I'm trying to create a sum of count of events in the past 30 days:
select key, sum((COALESCE(count(*),0)))
from table
Where date>= '2016-08-13'
And date<= '2016-09-11'
group by key;
but the sum doesn't seem to work. i'm looking at the last 30 days, and i would like to count any row that exists for each key, and then sum the counts (i need to count on a daily basis and then sum all day's count).
If you can offer any other way to deal with this issue i'm open for suggestions!
Many thanks,
Shira
You can't nest aggregate functions in HQL (or SQL). However, if you just want a count of records falling within range for each key, then you can simply just use COUNT(*):
select key, count(*)
from table
where date >= '2016-08-13' and
date <= '2016-09-11'
group by key;
It looks like there was a couple things wrong with your code.
I've written this for you, haven't tested it but it passes the syntax test.
SELECT COUNT(key) AS Counting FROM tblname
WHERE date>= '2016-08-13'
AND date<= '2016-09-11'
GROUP BY key;
And this might help you. You should definitely be using COUNT for this query.
I'm not sure if it's related but there might be an issue with calling a field 'key' I kept receiving syntax errors for it.
Hope I was able to help!
-Krypton

Neo4j: how to query by intermediate date when given date range

Neo4J TimeTree is an efficient way of modelling time in a graph. However, I'm interested in how best to model/query for an object with a defined start and end time.
For instance, a ticket might be validFrom and validTo given dates, which may be separated by many days. A user may have many tickets.
For a given date, what is the most efficient way of querying for valid tickets?
When entering the data, I suppose I could create lots of validOn relationships between a ticket and the intermediate days between the start and end, but this seems inefficient. Can anyone think of a better way of querying the data?
I can start from a user and find all tickets for that user whose validFrom is <= and validTo is >= the date. However, what happens if I need to start from a date? I.e. match all tickets that are valid on a given date?
You only link the ticket to the validFrom and validTo dates with dedicated relationships.
For any given day, you query backwards for tickets that have their :START relationship before that date but the :END relationship after that date, something like this:
MATCH path = (t:Ticket)-[:START]->(before:Day)-[:NEXT*0..30]->(day:Day {date:{date}})
WHERE (t)-[:END]->(:Day)<-[:NEXT*1..30]-(day)
RETURN t

Query that discards duplicate keys with parse.com SDK?

I'm researching how to implement leaderboards for my game with the parse.com SDK, my plan is to submit a score for the user every time they finish a level, attached to a "parent" leaderboard. I need to submit all scores because I need to retrieve leaderboards within time ranges (eg "all time", "last week", "last month", etc). The problem is, there'll be multiple scores for each user on the same leaderboard, and I only need to highest one. Is there a way to drop duplicate keys from a query? Is this the correct strategy? Everything else (sorting, paging, etc) seems to be in place.
Thanks.
You just need a table with 'User_id', 'Score', 'Level', and 'Date' (or whatever you need). Each time that a player finishes a level, you put the score into the table.
Then you have to calculate each (all time, last week, etc) in the query.
E.g.
10 Highs of the day:
SELECT TOP 10 User_id, Score, Date FROM Scores
WHERE Date = getdate()
ORDER BY Score DESC
I don't know if I understood the question. Let me know if I didn't.
Hope it helps.
As far as I understand from your question you want to retrieve data from Parse class. At the same time you want to eliminate the duplicate entry because user has multiple scores in different days. So to get the highest one, you have to query the class via query (based on SDK Android,iOS) and order by descending(based on your criteria), then obtain the first item in the result.
Or you can get the user all scores and create a structure where you can store the user scores as array list day by day. Based on day you can get the latest max or min scores. I hope I understand your question.
Hope this helps.Regards

Linq Lambda sort on DateField

Today something strange happened when I was sorting a list using Linq + lambda.
I have a list with articles that should be OrderedByDescending on a Field c => c.Statistics.Created. This field comes back as a DateTime. I do the following:
newsItems = newsItems.OrderByDescending(c => c.Statistics.Created).ToArray<Item>();
I understand that this list will be ordered in a descending direction and that the time of this date is also being watched as this is just a DateTime object. Now the weird part. I adjust the code to look the following:
newsItems = newsItems.OrderByDescending(c => c.Statistics.Created.Date).ToArray<Item>();
Now when I look at the list, the items are still ordered the way they should be. I can't understand why the list is still ordered the right way, when I tell Linq to only look at the date. Somehow Linq decides to look at the time aswell. When I attach my application to the w3p process and watch what happens I see that the Time is actually empty on the Date object, like I expect.
Can someone explain me how this list actually is getting sorted on time when I tell linq to only look at the date?
Already solved!
I have already found what made this strange behavior occur. It was that the list (gotten out of a CMS) was already ordered some way. When I then sorted on the date (with no time) the order was apperently correct an Linq did no do anything else with this date.

Find records that were created closest to the current date

I would like to get the records that have their created_at date closest to the current date. How can I do this with active records' where clause?
You could find the closest record in the past with something like:
Record.where("created_at <= ?", Date.today).order_by("created_at DESC").limit(1)
Similarly, you can have the closest record in the future
Record.where("created_at >= ?", Date.today).order_by("created_at ASC").limit(1)
And then compare wich one is the closest to current date...
There may be a solution to do it with a single request, but I could not find how (if you're using SQL server, there's a method DATEDIFF that could help).
Update: Thanks to Mischa
If you're sure that all created_atare in the past, you're looking to the last created record, that could be written
Record.order("created_at").last
Update
To get all the records created the same date then the last record:
last_record_date = Record.max(:created_at)
Record.where(:created_at => (last_record_date.at_beginning_of_day)..(last_record_date.end_of_day))

Resources