Maximum in group by active record query
Date : March 29 2020, 07:55 AM
this one helps. I have a Model which has two attributes (I am showing only two as only those two columns are needed) , If you are specifying a place first you can do: MyModel.where("place_id=?",place_id).
group("place_id,user_id").order("count(user_id) DESC").first
MyModel.select("DISTINCT(place_id)").
merge(MyModel.group("place_id,user_id").order("count(user_id) DESC"))
|
Combining Active Record group, join, maximum & minimum
Tag : sql , By : snapshooter
Date : March 29 2020, 07:55 AM
may help you . I'm trying to get to grips with the Active Record query interface. I have two models: , I think this will allow you to calculate maximum per genre: Movie.joins(:datapoints).where(datapoints: {timestamp: (Time.now)..(Time.now+1.year)}).group(:genre).maximum(:cumulative_downloads)
rel = Movie.joins(:datapoints).where(datapoints: {timestamp: (Time.now)..(Time.now+1.year)}).group(:genre)
mins = rel.minimum(:cumulative_downloads)
maxs = rel.maximum(:cumulative_downloads)
res = {}
maxs.each{|k,v| res[k] = v-mins[k]}
# get the genre and diff per movie
result = Movie.select('movies.genre, MAX(datapoints.cumulative_downloads)-MIN(datapoints.cumulative_downloads) as diff').joins(:datapoints).group(:movie_id)
# sum the diffs per genre
per_genre = Hash.new(0)
result.each{|m| per_genre[m.genre] += m.diff}
# get the genre and diff per movie
result = Movie
.select('movies.movie_id, movies.genre, MAX(datapoints.cumulative_downloads)-MIN(datapoints.cumulative_downloads) as diff')
.joins(:datapoints)
.group('movies.movie_id, movies.genre')
# sum the diffs per genre
per_genre = Hash.new(0)
result.each{|m| per_genre[m.genre] += m.diff}
|
select maximum n records from table with at most one record per group
Tag : mysql , By : lifchicker
Date : March 29 2020, 07:55 AM
will help you I have a table with a simple schema: user_id, score and I would like to extract the n records with the highest score, such that each user_id is only represented once. SELECT user_id, MAX(score) FROM table GROUP BY user_id ORDER BY MAX(score) DESC LIMIT 3;
|
Group by Total per week, and only show record with the maximum sum (Access)
Date : March 29 2020, 07:55 AM
I hope this helps you . Didn't know RDBMS so I assumed SQL SERVER and therefore that a CTE (Common Table Expression) would work. We can avoid the cte by using subqueries if needed though... Also I eliminated spaces in column names as I didnt' know if I should use ` or [ or " to escape the field names.... With CTE AS (SELECT ProjectName, Origin, Week, count(projectname) as myCount
FROM FOO
GROUP BY ProjectName, Origin, week)
SELECT A.ProjectName, A.Origin, A.Week, A.MyCount
FROM CTE A
INNER JOIN (SELECT ProjectName, Origin, Max(myCount) MaxMyCount
FROM CTE
GROUP BY ProjectName, Origin) B
on A.Projectname = B.ProjectName
and A.Origin = B.Origin
and A.myCount = B.MaxMyCount
ORDER BY ProjectName, Origin, Week
|
Mysql Agregate function to select maximum and then select minimum price within that group
Tag : mysql , By : Sigtryggur
Date : March 29 2020, 07:55 AM
I wish did fix the issue. This question is asked and answered with tedious regularity in SO. If only the algorithm was better at spotting duplicates. Anyway... SELECT x.*
FROM my_table x
JOIN
( SELECT discount,MIN(price) min_price FROM my_table GROUP BY discount) y
ON y.discount = x.discount
AND y.min_price = x.price;
|