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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:48:35+00:00 2026-05-25T18:48:35+00:00

I am using SSMS 2008, trying to select just one row/client. I need to

  • 0

I am using SSMS 2008, trying to select just one row/client. I need to select the following columns: client_name, end_date, and program. Some clients have just one client row. But others have multiple.

For those clients with multiple rows, they normally have different end_date and program. For instance:

CLIENT       PROGRAM        END_DATE
a            b              c
a            d              e
a            f              g
h            d              e
h            f              NULL

This is a real simplified version of the actual data. As you will see, different clients can be in the same program (“d”). But the same client cannot be in the same program more than one time.

Also the tricky thing is that the end_date can be NULL, so when I tried selecting those clients with > 1 row, I added a HAVING statement > 1. But this eliminated all of my NULL End_date rows.

To sum up, I want one row per client. So those clients with only one row total + those clients listed above with the following criteria:

  • Select only the row where either the End_date is greatest or NULL. (In most cases the end_date is null for these clients).

How can I achieve this with as little logic as possible?

  • 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-25T18:48:36+00:00Added an answer on May 25, 2026 at 6:48 pm

    On SQL Server 2005 and up, you can use a Common Table Expression (CTE) combined with the ROW_NUMBER() and PARTITION BY function. This CTE will “partition” your data by one criteria – in your case by Client, creating a “partition” for each separate client. The ROW_NUMBER() will then number each partition ordered by another criteria – here I created a DATETIME – and assigns numbers from 1 on up, separately for each partition.

    So in this case, ordering by DATETIME DESC, the newest row gets numbered as 1 – and that’s the fact I use when selecting from the CTE. I used the ISNULL() function here to assign those rows that have a NULL end_date some arbitrary value to “get them in order”. I wasn’t quite sure if I understood your question properly: did you want to select the NULL rows over those with a given end_Date, or did you want to give precedence to an existing end_Date value over NULL?

    This will select the most recent row for each client (for each “partition” of your data):

    DECLARE @clients TABLE (Client CHAR(1), Program CHAR(1), END_DATE DATETIME)
    
    INSERT INTO @clients 
    VALUES('a', 'b', '20090505'),
    ('a', 'd', '20100808'),
    ('a', 'f', '20110303'),
    ('h', 'd', '20090909'),
    ('h', 'f', NULL)
    
    ;WITH LatestData AS
    (
       SELECT Client, Program, End_Date,
           ROW_NUMBER() OVER(PARTITION BY CLient ORDER BY ISNULL(End_Date, '99991231') DESC) AS 'RowNum'
        FROM @clients
    )
    SELECT Client, Program, End_Date
    FROM LatestData 
    WHERE RowNum = 1
    

    Results in an output of:

    Client  Program  End_Date
       a       f     2011-03-03
       h       f     (NULL)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SSMS 2008 and trying to concatenate one of the rows together
I am using SSMS 2008 and am trying to select count of consumers who
I am using SSMS 2008 and am trying to write a trigger now to
I am using SSIS 2008 and am trying to update one column in my
This should be really simple. I am using SSMS 2008, trying to get a
I am using SSMS 2008 and trying to use a HAVING statement. This should
I am using SSMS 2008 and am trying to write a recursive replace statement.
I am using SSMS 2008 and I have the following Scalar function to take
I am using SSMS 2008 and I have the following scalar function to take
I am using SSMS 2008 and trying to insert with this query but am

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.