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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:48:38+00:00 2026-06-06T04:48:38+00:00

Table structure id (p) | date | id2 (fk) | id3 (fk) where (p)

  • 0

Table structure

id (p) | date | id2 (fk) | id3 (fk)

where (p) is primary id and (fk) is foreign key

This query returns a list of latest unique rows

select
    max(date) as date1,
    id1,
    id2
from `table`
group by id1, id2

I would also like to have the second date in a row, which have to be the second highest date

Something like

select
    max(date) as date1,
    max_second(date) as date2,
    id1,
    id2
from `table`
group by id1, id2
  • 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-06T04:48:40+00:00Added an answer on June 6, 2026 at 4:48 am

    Join the table to itself, matching the id columns but (and this is the key) matching where the joined table’s rows have dates less than the main table’s rows.

    select
        max(t1.date) as date1,
        max(t2.date) as date2,
        t1.id2,
        t1.id2
    from `table` t1
    left join `table` t2 on t2.id1 = t1.id1 and t2.id2 = t1.id2 and t2.date < t1.date
    group by id1, id2;
    

    Note that date2 may null if there are no rows for a previous date (hence the need for a left join)

    This query will take forever unless you have an index on id1 and/or id2.
    For maximum speed, do this:

    create index table_id1_id2 on `table`(id1, id2);
    

    and if that’s not fast enough, try the speed after running this:

    create index table_id1_id2_date on `table`(id1, id2, date);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table containing 60 million rows. The structure is like entryid, date,
With the following table structure: Table 1: Customers CustID(primary key) | CustName(indexed) ---------------------------------- C1
I have table structure like this EnrollNo,Date,Time 1, 8-10-2011 12:00:32 , 13:12 1, 8-10-2011
I have a table with this structure: date colname value ---------------------------- date col1name col1's
Table structure- int PrimaryKey DateTime Date int Price What I would like to do
Structure of my table : ID(int) | NUMBER(int) | CREATED_AT(date) If I have three
Table structure goes something like this: Table: Purchasers Columns: id | organization | city
Simplified table structure (the tables can't be merged at this time): TableA: dts_received (datetime)
I have this kind of table for simple chat: messages table structure +----+---------+-----------+---------+------+------+ |
I have a huge table containing 161 million rows. The table structure is entryid(varchar),

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.