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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:55:26+00:00 2026-05-31T16:55:26+00:00

Suppose i have a mysql table name table with fields rank date id The

  • 0

Suppose i have a mysql table name table with fields

  • rank
  • date
  • id

The values are like:

10, 2012-01-01, 3
9,  2012-01-04, 3
5,  2012-01-07, 3
3,  2012-01-10, 3
10, 2012-01-01, 4 
6,  2012-01-04, 4 
7,  2012-01-07, 4

In a single sql, how can i get both last and first values sorted by date grouped by id?

I know how to get first one or last one

SELECT rank, id FROM 
  (SELECT rank, id FROM table ORDER BY date DESC) s GROUP BY id;

I would like that the fields returned to be somethink like: lastrank, firstrank and id.

Thank you

  • 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-05-31T16:55:28+00:00Added an answer on May 31, 2026 at 4:55 pm

    Try this:

    select id,
      max(if(MyOrder = 1, rank, null)) as FirstRank,
      max(if(MyOrder = 2, rank, null)) as LastRank
    from (
        select t1.id, t1.rank, 1 MyOrder from t t1
        left join t t2 on
        t1.id = t2.id and t1.date > t2.date
        where t2.date is null
        union
        select t1.id, t1.rank, 2 from t t1
        left join t t2 on
        t1.id = t2.id and t1.date < t2.date
        where t2.date is null
    ) s
    group by id
    

    The result of this query taking your sampla data as input is:

    +----+-----------+----------+
    | ID | FIRSTRANK | LASTRANK |
    +----+-----------+----------+
    |  3 |        10 |        3 |
    |  4 |        10 |        7 |
    +----+-----------+----------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a simple MySQL table that looks like this: CREATE TABLE `my_table`
How can I sort into MySQL in AlphaNumeric order; Suppose I have a table
Suppose I have a table (MySQL) like this: CREATE TABLE sessions ( session_id INT
Suppose I have a table as follows in MySQL: create table user(name varchar(20), join_time
Suppose I have this table [Table1] Name Mark ------- ------ ABC 10 DEF 10
I have a MySQL table by name 'videos', where one of the column is
Suppose I have a command like this: select name, age, haircolor from people where
Suppose we have this person table id name gender 1 John male 2 Kurt
Suppose I have the following student table: id | name | datecreated ------------------------ 1
Suppose I have a MySQL table that keeps the page visit count and I

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.