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

  • Home
  • SEARCH
  • 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 6148873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:21:17+00:00 2026-05-23T19:21:17+00:00

All right, so here’s a challenge for all you SQL pros: I have a

  • 0

All right, so here’s a challenge for all you SQL pros:
I have a table with two columns of interest, group and birthdate. Only some rows have a group assigned to them.
I now want to print all rows sorted by birthdate, but I also want all rows with the same group to end up next to each other. The only semi-sensible way of doing this would be to use the groups’ average birthdates for all the rows in the group when sorting. The question is, can this be done with pure SQL (MySQL in this instance), or will some scripting logic be required?

To illustrate, with the given table:

id | group | birthdate
---+-------+-----------
1  | 1     | 1989-12-07
2  | NULL  | 1990-03-14
3  | 1     | 1987-05-25
4  | NULL  | 1985-09-29
5  | NULL  | 1988-11-11

and let’s say that the “average” of 1987-05-25 and 1989-12-07 is 1988-08-30 (this can be found by averaging the UNIX timestamp equivalents of the dates and then converting back to a date. This average doesn’t have to be completely correct!).
The output should then be:

id | group | birthdate  | [sort_by_birthdate]
---+-------+------------+--------------------
4  | NULL  | 1985-09-29 | 1985-09-29
3  | 1     | 1987-05-25 | 1988-08-30
1  | 1     | 1989-12-07 | 1988-08-30
5  | NULL  | 1988-11-11 | 1988-11-11
2  | NULL  | 1990-03-14 | 1990-03-14

Any ideas?

Cheers,
Jon

  • 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-23T19:21:17+00:00Added an answer on May 23, 2026 at 7:21 pm

    I normally program in T-SQL, so please forgive me if I don’t translate the date functions perfectly to MySQL:

    SELECT
        T.id,
        T.group
    FROM
        Some_Table T
    LEFT OUTER JOIN (
        SELECT
            group,
            '1970-01-01' +
                INTERVAL AVG(DATEDIFF('1970-01-01', birthdate)) DAY AS avg_birthdate
        FROM
            Some_Table T2
        GROUP BY
            group
        ) SQ ON SQ.group = T.group
    ORDER BY
        COALESCE(SQ.avg_birthdate, T.birthdate),
        T.group
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

HI all. You can see the code right here: http://jsbin.com/egixa5/5/edit . I'm trying to
I've seem to implement all the right code here. Is there something I'm missing
All right all, So I've got a UITableView that is inited in applicationDidFinishLaunching like
I'm using the OnIdle-event for some simple animations, and it works all right. The
you all know: right click -> zoom in or out in flash file, well,
Right now all of my times on my reports are in GMT. Anybody know
Right now all I am using to calculate the size are the files in
The idea is to move all of the right elements into the left and
I'm developing a facebook app right now all by my lonesome. I'm attempting to
The WebBrowser control has a property called IsWebBrowserContextMenuEnabled that disables all ability to right-click

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.