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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:32:26+00:00 2026-05-19T06:32:26+00:00

I am attempting to narrow results of an existing complex query based on conditional

  • 0

I am attempting to narrow results of an existing complex query based on conditional matches on multiple columns within the returned data set. I’ll attempt to simplify the data as much as possible here.

Assume that the following table structure represents the data that my existing complex query has already selected (here ordered by date):

+----+-----------+------+------------+
| id | remote_id | type | date       |
+----+-----------+------+------------+
|  1 |         1 | A    | 2011-01-01 |
|  3 |         1 | A    | 2011-01-07 |
|  5 |         1 | B    | 2011-01-07 |
|  4 |         1 | A    | 2011-05-01 |
+----+-----------+------+------------+

I need to select from that data set based on the following criteria:

  • If the pairing of remote_id and type is unique to the set, return the row always
  • If the pairing of remote_id and type is not unique to the set, take the following action:
    • Of the sets of rows for which the pairing of remote_id and type are not unique, return only the single row for which date is greatest and still less than or equal to now.

So, if today is 2011-01-10, I’d like the data set returned to be:

+----+-----------+------+------------+
| id | remote_id | type | date       |
+----+-----------+------+------------+
|  3 |         1 | A    | 2011-01-07 |
|  5 |         1 | B    | 2011-01-07 |
+----+-----------+------+------------+

For some reason I’m having no luck wrapping my head around this one. I suspect the answer lies in good application of group by, but I just can’t grasp it. Any help is greatly appreciated!

  • 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-19T06:32:27+00:00Added an answer on May 19, 2026 at 6:32 am
    /* Rows with exactly one date - always return regardless of when date occurs */
    SELECT id, remote_id, type, date
        FROM YourTable
        GROUP BY remote_id, type
        HAVING COUNT(*) = 1
    UNION
    /* Rows with more than one date - Return Max date <= NOW */
    SELECT yt.id, yt.remote_id, yt.type, yt.date
        FROM YourTable yt
            INNER JOIN (SELECT remote_id, type, max(date) as maxdate
                            FROM YourTable
                            WHERE date <= DATE(NOW())
                            GROUP BY remote_id, type
                            HAVING COUNT(*) > 1) sq
                 ON yt.remote_id = sq.remote_id
                     AND yt.type = sq.type
                     AND yt.date = sq.maxdate
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Attempting to set up an automated texting service for customers, where people can text
Attempting to use the data series from this example no longer passes the JSONLint
Attempting/struggling to get registration and sign-up working within an active admin project. I have
Attempting to run a MIB based SNMP script and I running into an issue.
Attempting to set up Samba + OpenLDAP using nss_ldap. After joining Windows7 to Samba
Attempting to use the amazon API to obtain product data and currently failing miserably.
Attempting to create a filter select to find schedules with coursedates within a given
Attempting to deserialize JSON data and update each object's prototype and inherit a common
Attempting to run adt from an SSH session results in: Exception in thread main
Attempting to set the tabIndex for date_select has proven to be difficult out of

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.