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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:17:46+00:00 2026-06-05T21:17:46+00:00

I have a table with the following columns: ref_year, ref_no, inv_id, start_date, end_date The

  • 0

I have a table with the following columns:

ref_year, ref_no, inv_id, start_date, end_date

The first two columns (ref_year and ref_no) combine to form a primary key in other tables and I will refer to them as ‘reference’ from now on, but in this table they can appear multiple times. The third (inv_id) is a foreign key. The final two columns represent the date an inv_id became attached to the reference and, where appropriate, the date it ceased being attached to that reference.

I want to return exactly one row for each reference that will relfect the earliest inv_id attached to that reference where end_date is null. It’s the end_date part that’s causing me problems. Here’s what I’ve got so far:

SELECT
    t1.*
FROM
    involvements t1
LEFT OUTER JOIN
    involvements t2
    ON
        (t1.ref_year = t2.ref_year
    AND
        t1.ref_no = t2.ref_no
    AND
        t1.start_date < t2.start_date)
WHERE
    t2.ref_year IS NULL
AND
    t2.ref_no IS NULL

This selects the inv_id with the earliest start_date perfectly, but I can’t figure out how to account for those cases where the inv_id with the earliest start_date has an end_date that is not null, in which case I’d want the script to check the next-oldest inv_id for that reference instead, and so on until it returns one with a null end_date. I tried creating a temporary table with only null end_dates then inner joining to this as a sub-query but of course couldn’t because the WHERE clause came before the sub-query. Is there an efficient way to get my desired behaviour?

  • 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-05T21:17:48+00:00Added an answer on June 5, 2026 at 9:17 pm

    I would use ROW_NUMBER() to pick the earliest record for each reference.

    WITH
      sequenced_data
    AS
    (
      SELECT
        ROW_NUMBER() OVER (PARTITION BY ref_year, ref_no ORDER BY start_date ASC) AS sequence_id,
        *
      FROM
        involvements
      WHERE
        end_date IS NULL
    )
    SELECT
      *
    FROM
      sequenced_data
    WHERE
      sequence_id = 1
    

    If a reference doesn’t have any records where end_date IS NULL, then it won’t return anything for that reference.

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

Sidebar

Related Questions

I have a table with the following columns: user_id, key, value, and flag. The
Lets say I have table with following columns 1. Client - string. 2. Profit
I have a table with following columns: ContractorId ......... INT ............. IDENTITY ContractorName ........
I have a table with the following columns in SQL Server: MEMBERID, MEMBEREMAIL, FATHEREMAIL,
I have a table with the following columns (I simplified it a little): PRODUCT_name
I have a table with the following columns: EntityId, EntityName, EntityProfile, ................. I want
Lets say I have a table with the following columns: Qty, INTEGER SaleDate, DATETIME
I have a table that contains the following columns: ID int, DISTANCE float, EVENT
I have created a table with the following columns: ObservationId, FirstCreatedDate, description, ... ...
i have users_details table with the following columns martial_status | religion | education i

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.