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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:26:25+00:00 2026-06-13T10:26:25+00:00

I have a table that reflects a monthly census of a certain population. Each

  • 0

I have a table that reflects a monthly census of a certain population. Each month on an unpredictable day early in that month, the population is polled. Any member who existed at that point is included in that month’s poll, any member who didn’t is not.

My task is to look through an arbitrary date range and determine which members were added or lost during that time period. Consider the sample table:

ID | Date
2  | 1/3/2010
3  | 1/3/2010
1  | 2/5/2010
2  | 2/5/2010
3  | 2/5/2010
1  | 3/3/2010
3  | 3/3/2010

In this case, member with ID “1” was added between Jan and Feb, and member with ID 2 was lost between Feb and Mar.

The problem I am having is that if I just poll to try and find the most recent entry, I will capture all the members that were dropped, but also all the members that exist on the last date. For example, I could run this query:

SELECT
  ID,
  Max(Date)
FROM
  tableName
WHERE
  Date BETWEEN '1/1/2010' AND '3/27/2010'
GROUP BY
  ID

This would return:

ID  | Date
1   | 3/3/2010
2   | 2/5/2010
3   | 3/3/2010

What I actually want, however, is just:

ID  | Date
2   | 2/5/2010

Of course I can manually filter out the last date, but since the start and end date are parameters I want to generalize that. One way would be to run sequential queries. In the first query I’d find the last date, and then use that to filter in the second query. It would really help, however, if I could wrap this logic into a single query.

I’m also having a related problem when I try to find when a member was first added to the population. In that case I’m using a different type of query:

SELECT
  ID,
  Date
FROM
  tableName i
WHERE
  Date BETWEEN '1/1/2010' AND '3/27/2010'
 AND
  NOT EXISTS(
    SELECT
      ID,
      Date
    FROM
      tableName ii
    WHERE
      ii.ID=i.ID
     AND
      ii.Date < i.Date
     AND
      Date BETWEEN '1/1/2010' AND '3/27/2010'
  )

This returns:

ID  | Date
1   | 2/5/2010
2   | 1/1/2010
3   | 1/1/2010

But what I want is:

ID  | Date
1   | 2/5/2010

I would like to know:
1. Which approach (the MAX() or the subquery with NOT EXISTS) is more efficient and
2. How to fix the queries so that they only return the rows I want, excluding the first (last) date.

Thanks!

  • 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-06-13T10:26:26+00:00Added an answer on June 13, 2026 at 10:26 am

    You could do something like this:

    SELECT
      ID,
      Max(Date)
    FROM
      tableName
    WHERE
      Date BETWEEN '1/1/2010' AND '3/27/2010'
    GROUP BY
      ID
    having max(date) < '3/1/2010'
    

    This filters out anyone polled in March.

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

Sidebar

Related Questions

I have a text file that I parse each month and insert the data
I have table that I insert data with following query (from c# code): INSERT
I have table that contain date and time field. id|date|time ========= 1|01/01/2001|10:45 2|01/02/2002|11:45 3|01/03/2003|12:45
I have a table that contains multiple dropdown menus for a list of profile
I have a table that looks like this: id value AGA 0.211 AGA 0.433
I have a table that looks like this: CREATE TABLE foobar ( id SERIAL
I have a table that acts as a dropbox for files that get automatically
I have a table that has account numbers in (account_num) and user profiles (profile_id).
I have a table that stores data that has been entered regarding the amount
I have a table that looks like this: ID (pk,int) Col1 (nvarchar) Col2 (nvarchar)

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.