question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Best way to select row of a max record in group?

See original GitHub issue

a 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:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
yosagarcommented, Nov 5, 2019

I managed to get the required result as below

Model.findAll({
        where: {
          id: { 
            [Sequelize.Op.in]: [Sequelize.literal('SELECT MAX(id) FROM a GROUP BY groupId')]
          }
        },
      });
4reactions
janmeiercommented, Aug 15, 2016

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found