Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7847625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:57:54+00:00 2026-06-02T17:57:54+00:00

I am having a hard time doing this without searching the same table at

  • 0

I am having a hard time doing this without searching the same table at least twice in order to grab the max row, and then grab the value for that row. The table in question is quite big so this is unacceptable.

Here is what my table might look like:

SCORES
ID    ROUND    SCORE
1     1        3
1     2        6
1     3        2
2     1        10
2     2        12
3     1        6

I need to return the score that each ID got in the most recent round. That is, the row with the max (round), but not the max score.

OUTPUT:
ID   ROUND   SCORE
1    3       2
2    2       12
3    1       6

Right now I have:

SELECT * FROM 
(SELECT id, round,
CASE WHEN (MAX(round) OVER (PARTITION BY id)) = round THEN score ELSE NULL END score
 FROM
 SCORES
 where id in (1,2,3)
) scorevals
WHERE
scorevals.round is not null;

This works, but is pretty inefficient (I have to manually filter out all of these rows, when I should just be able to not grab those rows in the first place.)

What can I do to get the right values?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:57:57+00:00Added an answer on June 2, 2026 at 5:57 pm

    This is also possible without subquery:

    SELECT DISTINCT
           id
          ,max(round) OVER (PARTITION BY id) AS round
          ,first_value(score) OVER (PARTITION BY id ORDER BY round DESC) AS score
    FROM   SCORES
    WHERE  id IN (1,2,3)
    ORDER  BY id;
    

    Returns exactly what you asked for.
    The crucial point is that DISTINCT is applied after window functions.

    SQL Fiddle.

    Maybe faster because it uses the same window twice:

    SELECT DISTINCT
           id
          ,first_value(round) OVER (PARTITION BY id ORDER BY round DESC) AS round
          ,first_value(score) OVER (PARTITION BY id ORDER BY round DESC) AS score
    FROM   SCORES
    WHERE  id IN (1,2,3)
    ORDER  BY id;
    

    Otherwise doing the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a hard time doing this simple thing: return 0 instead of null SELECT
I'm having a hard time doing this query. I want to compare dates in
I'm having a hard time with a selectManyCheckbox. Basically what I am doing is
I'm doing a BigInt implementation in C++ and I'm having a hard time figuring
I'm doing a JSF Composite, but I m having a hard time with including
I'm having hard time with this simple ajax call function sendreq() { $.ajax({ dataType:
I'm having a hard time wrapping my head around this and need some help
im trying to translate this code to python, but im having a hard time
I am having a hard time getting this to work file: myclass.hpp Class MyClass
I'm having hard time trying to set certain cell's value in table. Here is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.