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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:05:22+00:00 2026-05-11T20:05:22+00:00

I have a query which does a number of joins and has a few

  • 0

I have a query which does a number of joins and has a few criteria in the WHERE clause, and I’m ending up with a result which essentially looks like this:

| userId |       date |  otherData |
|--------+------------+------------|
|      1 | 2008-01-01 |  different |
|      1 | 2009-01-01 |       info |
|      1 | 2010-01-01 |        for |
|      2 | 2008-01-01 |       each |
|      3 | 2008-01-01 |        row |
|      3 | 2009-01-01 |       here |

So, in essence for each user, there will be one or more dates in the past, and 0 or more dates in the future.

I need to somehow reduce the dataset to one row per user, only selecting the row which has the most recently passed date. That is, with whatever magic GROUP BY or HAVING clause is added, the result from above would look like this:

| userId |       date |  otherData |
|--------+------------+------------|
|      1 | 2009-01-01 |       info |
|      2 | 2008-01-01 |       each |
|      3 | 2009-01-01 |       here |
  • 1 1 Answer
  • 1 View
  • 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-11T20:05:22+00:00Added an answer on May 11, 2026 at 8:05 pm

    I think you don’t want to use GROUP BY / HAVING because you are interested in exactly 1 row per user, and that row already exists in the table as-is. This calls for a WHERE clause and not GROUP BY / HAVING.

    My suggestion is that in the WHERE clause, you add a condition that the date must be equal to the result of a subquery. That subquery should:

    1. Select the max(date)
    2. Have a WHERE clause that limits date to be smaller than NOW
    3. Have a WHERE clause with UserID equals outer query’s user id

    To prevent cases where a certain user can have two entries with the same date that’s also the maximum “passed” date, you should also add DISTINCT.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a result from a query like the below, which does not have
I have a query which looks like this: SELECT LossCost, CoverageID FROM BGILossCost] WHERE
I have a query which produces a list of orders like this: Invoice Description
Possible Duplicate: Row Offset in SQL Server I have a query which has a
I have a stored procedure which does sorting pnd Paging like the Sorting Custom
I have a rails time-based query which has some odd timezone sensitive behaviour, even
I have a query which aims to retrieve a random row from a result
I have this query which on executing in my sql command line client executes
I have a query which returns two rows of data as count. I want
I have this query which is a dependant query and taking much execution time

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.