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 8901501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:22:19+00:00 2026-06-15T01:22:19+00:00

I have a table with id , year and count . I want to

  • 0

I have a table with id, year and count.

I want to get the MAX(count) for each id and keep the year when it happens, so I make this query:

SELECT id, year, MAX(count)
FROM table
GROUP BY id;

Unfortunately, it gives me an error:

ERROR: column “table.year” must appear in the GROUP BY clause or be
used in an aggregate function

So I try:

SELECT id, year, MAX(count)
FROM table
GROUP BY id, year;

But then, it doesn’t do MAX(count), it just shows the table as it is. I suppose because when grouping by year and id, it gets the max for the id of that specific year.

So, how can I write that query? I want to get the id´s MAX(count) and the year when that happens.

  • 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-15T01:22:20+00:00Added an answer on June 15, 2026 at 1:22 am
    select *
    from (
      select id, 
             year,
             thing,
             max(thing) over (partition by id) as max_thing
      from the_table
    ) t
    where thing = max_thing
    

    or:

    select t1.id,
           t1.year,
           t1.thing
    from the_table t1
    where t1.thing = (select max(t2.thing) 
                      from the_table t2
                      where t2.id = t1.id);
    

    or

    select t1.id,
           t1.year,
           t1.thing
    from the_table t1
      join ( 
        select id, max(t2.thing) as max_thing
        from the_table t2
        group by id
      ) t on t.id = t1.id and t.max_thing = t1.thing
    

    or (same as the previous with a different notation)

    with max_stuff as (
      select id, max(t2.thing) as max_thing
      from the_table t2
      group by id
    ) 
    select t1.id, 
           t1.year,
           t1.thing
    from the_table t1
      join max_stuff t2 
        on t1.id = t2.id 
       and t1.thing = t2.max_thing
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query: SELECT COLUMN1, COLUMN2, COUNT(*) FROM TABLE WHERE COLUMN3 IS
I have the following MySQL query: SELECT (COUNT(id) * 5) AS total FROM content
I have a table like: id number year I want to find holes, or
I have a table that looks like this: Name Job Year ImpFile ImpDate ------------------------------------------------
I have a table that looks something like this: Name Year Value A 2000
I have a table with countries, years and amounts for each year. I'm trying
I have a calendar table with data from year 2000 to 2012 (2012 wasn't
I have a column in a MySQL table called year. I want to check
I have a problem constructing a mysql query: I have this table tSubscribers were
So I have the following SELECT statements: SELECT COUNT(A.Award) AS US, SUBSTRING(CAST(M.Year as char(4)),

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.