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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:07:50+00:00 2026-06-02T13:07:50+00:00

I have a table (let’s call it tableA) that looks something like this: id

  • 0

I have a table (let’s call it tableA) that looks something like this:

    id    |   dateA    |   dateB    
----------|------------|-------------
    1     | 2011-01-01 | 2010-05-01
    1     | 2011-05-01 | 2010-06-12
    2     | 2011-01-11 | 2010-01-31
    2     | 2011-01-31 | 2010-02-01
    3     | 2011-05-11 | 2010-08-02
    3     | 2011-08-02 | 2010-09-10

My goal is to group rows by id, and get minimum of dateA and maximum of dateB
and get a table that looks something like this:

    id    | min(dateA) | max(dateB)
----------|------------|-------------
    1     | 2011-01-01 | 2010-06-12
    2     | 2011-01-11 | 2010-02-01
    3     | 2011-05-11 | 2010-09-11

Right now, I’m using LEFT join approach:

SELECT
    id,
    tableB.dateA,
    tableC.dateB
FROM tableA as a

    LEFT JOIN (
        SELECT id, min(dateA)
        FROM tableA
        GROUP BY id
    )tableB ON a.id = tableB.id

    LEFT JOIN (
        SELECT id, max(dateB)
        FROM tableA
        GROUP BY id
    )tableC ON a.id = tableC.id

However, my approach is way too slow. I have pretty big table, and it takes about 7seconds to get the desired outcome.

Could anyone suggest me a good optimizing technique to apply to my situation?

Thank you.

J

  • 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-02T13:07:51+00:00Added an answer on June 2, 2026 at 1:07 pm

    What’s wrong with a simple GROUP BY using one pass through the table and no self-joins?

    SELECT id, MIN(dateA), MAX(dateB)
      FROM TableA
     GROUP BY id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that looks like this - http://d.pr/jQ1P and then another table
Let's say I have a table like this: name | score_a | score_b -----+---------+--------
Let's say I have table like this: id: PRIMARY INT price: INT (in dollars)
Let's say I have table with column 'URL' whrere I store urls like this
Let's say I have a table that represents a super class, students . And
I have a table, let's call is [MYTABLE] , with an FOR INSERT, UPDATE
Let's say I have a table that has user_id, date, score, and every user
Let's say I have a table Foo . This table has the columns ID
Let's suppose I have the following table (let's call it my_table ): CREATE TABLE
Let say I have this table, A B ------- 1 A 2 C 3

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.