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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:04:08+00:00 2026-05-25T02:04:08+00:00

In my system, I have clients. Clients have programs. I want to display a

  • 0

In my system, I have clients. Clients have programs. I want to display a list of clients, showing their most recent active (if it exists) program.

Thus, we have something like this:

SELECT * 
FROM clients AS client 
    JOIN programs AS program ON client.id=program.client_id
GROUP BY client.id
ORDER BY program.close_date=0 DESC, program.close_date DESC

close_date=0 means the program isn’t closed. So it will put the non-closed programs first, and then the most recently closed programs next.

Problem is, the order by doesn’t work within the groups. It just kind of picks one of the programs at random. How do I resolve this?


Just came up with this:

SELECT * 
FROM clients AS client 
    JOIN (SELECT * FROM programs AS program ORDER BY program.close_date=0 DESC, program.close_date DESC) AS program ON client.id=program.client_id
GROUP BY client.id

Which seems to give correct results. Is this correct, or am I just getting lucky? i.e., I’ve essentially sorted the table before joining on it; those results will stay sorted as it does the join, right?


Solution: I now believe this a classic group-wise maximum problem. Search for that if you’re stuck on a similar problem. The solution involves joining the same table twice.

  • 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-25T02:04:09+00:00Added an answer on May 25, 2026 at 2:04 am
    SELECT  c.*, p.*
    FROM    clients AS c
    JOIN    programs AS p
    ON      p.id = 
            (
            SELECT  pi.id
            FROM    programs AS pi
            WHERE   pi.client_id = c.id
            ORDER BY
                    pi.close_date=0 DESC, pi.close_date DESC
            LIMIT 1
            )
    

    Thanx should go to @Quassnoi. See his answer in a similar (but more complicated) question: mysql-group-by-to-display-latest-result


    If you update the programs table and set close_date for all records that it is zero to close_date='9999-12-31', then your ORDER BY will be simpler (and the whole query faster with proper indexes):

            ORDER BY
                    pi.close_date DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a GridView with a list of clients and their details bound to
I have code for sending email to clients. System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient(); MailMessage
At work, we have a client-server system where clients submit requests to a web
I have a potential client that set up their website and membership system in
My client needs to have a banner management system installed to their site. They
Suppose I have one server and one client program. Clients have four options (square
I have a rich client program installed on users PCs where I want to
I have a system that takes Samples. I have multiple client threads in the
I have this system where a client uploads a logo of a company and
We have a system where our front end is either a Rich Client application

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.