Best way to select row of a max record in group?
See original GitHub issuea table has a groupId field, and a value field
How to select the full row record the max value in the group using sequelize?
I can only figure it out with SQL:
SELECT
a.*
FROM
table a
INNER JOIN
(SELECT
groupId, MAX(value) value
FROM
table
GROUP BY groupId) b ON a.groupId = b.groupId
AND a.value = b.value
GROUP BY a.groupId
and let sequelize run it.
const results = await sequelize.query(sqlSentence, { model: SiteContent })
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
5 Ways to Select Rows with the Maximum Value for their ...
Here are five options for using SQL to return only those rows that have the maximum value within their group.
Read more >SQL: Find the max record per group [duplicate] - Stack Overflow
select Name, Top, Total from sometable inner join ( select max(Total) Total, Name from sometable group by Name ) as max on max.Name...
Read more >MySQL select row with max value for each group - thisPointer
GROUP BY : MySQL select row with max value · Run the subquery to get only the maximum no_products_sold group by sale_person_name. ·...
Read more >How to Select the First Row in Each GROUP BY Group
First, you need to write a CTE in which you assign a number to each row within each group. To do that, you...
Read more >Get records with max value for each group of grouped SQL ...
You can try joining against a subquery that will be helpful to pull the MAX(Group) and Age. QUERY: SELECT t1.*. FROM yourTable t1....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I managed to get the required result as below
Currently we only support joins on relations defined in sequelize, and we don’t have great subquery support either https://github.com/sequelize/sequelize/issues/2787