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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:28:35+00:00 2026-05-28T00:28:35+00:00

With a table like: idx type dat 0 a foo1 1 b foo2 2

  • 0

With a table like:

idx type dat
0   a    foo1
1   b    foo2
2   c    foo3
3   a    foo4
4   b    foo5
5   c    foo6
6   a    foo7
7   b    foo8
8   c    foo9

How can i get the first and last data of each type:

for example:

a foo1 foo7
b foo2 foo8
c foo3 foo9

I have tried this query, but its way to slow, even with indexes on idx and type:

select mins.type, mins.dat, mytable.dat from mytable

--get the maximums
inner join
 (select max(idx) as maxidx from mytable group by type) as a
on a.maxidx = mytable.idx


--join the maximums to the minimums
inner join

--get the minimums
 (select * from mytable inner join (select min(idx) as minidx from mytable group by type) as b 
on b.minidx = mytable.idx ) as mins

on issues.type = mins.type

The join of on issues.type = mins.type seems to be what is slowing it down so much because mins.type is derived and not indexed?

  • 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-28T00:28:36+00:00Added an answer on May 28, 2026 at 12:28 am

    Use mysql’s “funky” functionality of a GROUP BY without aggregating the other columns, which simply returns the first row of the group. The problem then becomes getting the rows into the correct order before using this functionality, typically by using an aliased query.

    This approach avoids any correlated subqueries (queries per row) and needs just two passes over the table (one for each direction of ordering):

    select x2.type, x2.dat as first_dat, y2.dat as last_dat
    from (select *
      from (select type, dat
        from so8735514
        order by 1, 2) x1
      group by 1) x2
    join (select *
      from (select type, dat
        from so8735514
        order by 1, 2 desc) y1
      group by 1) y2 on y2.type = x2.type;
    

    Test code:

    create table so8735514 (idx int, type text, dat text);
    insert into so8735514 values
    (0, 'a', 'foo1'),
    (1, 'b', 'foo2'),
    (2, 'c', 'foo3'),
    (3, 'a', 'foo4'),
    (4, 'b', 'foo5'),
    (5, 'c', 'foo6'),
    (6, 'a', 'foo7'),
    (7, 'b', 'foo8'),
    (8, 'c', 'foo9');
    

    Output:

    +------+-----------+----------+
    | type | first_dat | last_dat |
    +------+-----------+----------+
    | a    | foo1      | foo7     |
    | b    | foo2      | foo8     |
    | c    | foo3      | foo9     |
    +------+-----------+----------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like this IDX IDY ColX ColY ... ------------------------------ idx1 idy1
SCENARIO: Imagine a TABLE like this: Auto_INC, UserID_FK, CaseID_FK, Date idX userX caseX and
I have a table with columns like these : idx | amount | usercol1
Table like datetime a1 b1 x2 ... 07-01-2009 13:10 8 9 10 07-01-2009 13:11
I have a table like as follows: SoftwareName Count Country Project 15 Canada Visio
If I have a table like: CREATE TABLE FRED ( recordId number(18) primary key,
Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this
If I have a table like this: CREATE TABLE sizes ( name ENUM('small', 'medium',
I have a table like this: <table> <tfoot> <tr><td>footer</td></tr> </tfoot> <tbody> <tr><td>Body 1</td></tr> <tr><td>Body
I have a SQL table like so: Update: I'm changing the example table as

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.