score:1
Another option would be to use the SQL DATEPART functions like this:
SELECT
DAY(TimeStamp), MONTH(TimeStamp), YEAR(TimeStamp),
DATEPART(HOUR, TimeStamp),
COUNT(*)
FROM
dbo.Products
GROUP BY
DAY(TimeStamp), MONTH(TimeStamp), YEAR(TimeStamp),
DATEPART(HOUR, TimeStamp)
ORDER BY
COUNT(*) DESC
This gives you not just the maximum number of views for any given hour, but all of them, sorted by the frequency. Mind you: other than RexM's solution, this is based on the "hour" part of your "timestamp" - so if you have quite a few views at 7:59 and another burst at 8:01, in my solution, those wouldn't be shown together (since one is hour=7 and the other is hour=8).
If you need the "any 60-minute timespan" approach, use RexM's basic idea (DATEDIFF with minutes <= 60).
Marc
score:0
If you're working with a set 60 minute time block (e.g. the last 60 minutes from now) then it's reasonably easy:
SELECT TOP 1
PR.ProductID,
COUNT(*)
FROM ProductReviews PR
WHERE PR.Timestamp BETWEEN DATEADD( minute, -60, GETDATE() ) AND GETDATE()
GROUP BY PR.ProductID
ORDER BY COUNT(*) DESC
If you're wanting it for any 60 minute interval then it gets more complex!
score:1
I don't know of an easy way to calculate that metric, but hopefully this will help. Without some sort of SQL cursor, I would generate a SQL table of possible intervals, with start and end timestamps (2009-09-02T00:00 to 2009-09-02T00:59, 2009-09-02T00:01 to 2009-09-02T01:00, etc) and then cross join using LINQ to SQL:
var rates = from r in db.Reviews
from i in db.Intervals
where i.Begin <= r.Timestamp && r.Timestamp <= i.End
group r by i.Begin into reviews
select reviews.Count();
var maxRate = rates.Max();
I haven't tried the code, but it should get you started. You could improve the performance by restricting how far back to check (last 7 days, 30 days, etc) or generate fewer intervals (starting on the quarter hour, perhaps).
score:1
If you're instead wanting "which product got the most reviews in a 60 minute block between dateX and dateY" then it gets a little more complex.
One way to think of it is "for each review in that time period how many other reviews are there for the same product ID in the following 60 minutes". Once you have that logic the query becomes clearer:
SELECT TOP 1
PR.ProductID,
-- start of 60 minute block
PR.Timestamp,
ReviewCount = (
SELECT COUNT(*)
FROM ProductReviews PR1
-- from parent time
WHERE PR1.Timestamp >= PR.Timestamp
-- until 60 mins later
AND PR1.Timestamp <= DATEADD( minute, 60, PR.Timestamp )
-- that matches ProductID
AND PR1.ProductID = PR.ProductID
)
FROM ProductReviews PR
-- in the last 24 hours
WHERE PR.Timestamp > ( GETDATE() - 1 )
ORDER BY ReviewCount DESC
Hows that?
score:0
If you were happy with fixed-hours for your windows, I may consider doing this a little bit smarter, by using a trigger. The trigger would insert/update into a 'log' table, and just differentiate between inserting or updating based on the current time.
You could combine with this any of the other approaches, it would add a nice caching layer.
Source: stackoverflow.com
Related Articles
- Finding maximum number of rows created per hour?
- EF Code First Insert Objects with One to Many gives exception."Store update, insert, or delete statement affected an unexpected number of rows (0)."
- finding number of occurences of any word in Rows
- how to find which value has the maximum number of rows in a table using LinQ?
- How to use Linq to group every N number of rows
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- Linq: How to group by maximum number of items
- Finding if a target number is the sum of two numbers in an array via LINQ
- What is faster in finding element with property of maximum value
- Get random number of rows from SQL Server table
- LINQ Source Code Available
- What is the Maximum Number of Records i can insert through InserAllOnSubmit() In Linq to Sql
- .NET 4 Code Contracts: "requires unproven: source != null"
- Maximum number of occurrences a character appears in an array of strings
- Returning a maximum number of items the LINQ way
- Pad the left or right sides of a linq join to be the same number of rows
- LINQ to count number of rows in link table
- creating Linq to sqlite dbml from DbLinq source code
- LINQ to DataTable, finding duplicate rows
- C# Finding the maximum value of a string with linq
- Using Linq to get the last N number of rows that have duplicated values in a field
- Finding if a target number is the sum of two numbers in an array via LINQ and get the and Indices
- Select Maximum number from a numeric field in an entity (Dynamics CRM 2016)
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- Searching for word and finding it's line number LINQ
- Finding maximum of Nth elements of a list of lists in C#
- Why the extension method of where for LINQ in this code would print out a single number while it shouldn't print anything at all?
- how can take row number 10 to 20 over 100+ rows using entity framework
- Count the number of Files modified in the last hour using linq
- Get maximum closest number from int list
- System.Linq.Dynamic.Core cannot cast 'object' type to 'Tuple<T1, T2, T3>'
- Is it possible to group data for 2 columns using Linq
- Null Object Reference on Retrieving Nested XML Elements with Linq / C#
- What is the best way to convert this SQL query to Linq?
- PostgreSQL, Linq C# error 'could not determine data type of parameter $1'
- LINQ : WHERE clause not working after GROUPBY AND SELECT
- Unable to cast object of type '<>f__AnonymousType5`6
- How to check DBNull into linq or int32?
- Querying for any entries after traversing multiple conditional includes in EF Core 5
- Link error of DBNull
- Using a data / row value as a column header using LINQ
- Linq GroupJoin with DefaultIfEmpty
- LINQ Left join Query
- Confused on Getting an int out of a var from Linq2Sql
- Using union inside a foreach loop for linq to sql
- Find matching items by complex combination of attributes
- LINQ driven cache items invalidation
- Avoid duplicate records in linq query while joining multiple tables
- LINQ filter list based on given conditions
- How to bind an Event Handler to a dynamically created control inside of a collection?