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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:45:59+00:00 2026-06-15T18:45:59+00:00

Image I have the following table with multiple codes for a single person for

  • 0

Image I have the following table with multiple codes for a single person for different periods (id is the primary key)

id   code   Name  Start                     Finish
325  1353   Bob   NULL                      2012-07-03 16:21:16.067
1742 1353   Bob   2012-07-03 16:21:16.067   2012-08-03 15:56:29.897
1803 1353   Bob   2012-08-03 15:56:29.897   NULL
17   575    Bob   NULL                      NULL
270  834    Bob   NULL                      2012-07-20 15:51:19.913
1780 834    Bob   2012-07-20 15:51:19.913   2012-07-26 16:26:54.413
1789 834    Bob   2012-07-26 16:26:54.413   2012-08-21 15:36:58.940
1830 834    Bob   2012-08-21 15:36:58.940   2012-08-24 14:26:05.890
1835 834    Bob   2012-08-24 14:26:05.890   2012-08-30 12:01:05.313
1838 123    Bob   2012-08-30 12:01:05.313   2012-09-05 09:29:02.497
1844 900    Bob   2012-09-05 09:29:02.497   NULL

What I want to do update the table such that the code is take from the latest person.

id   code   Name Start                      Finish
325  900    Bob  NULL                       2012-07-03 16:21:16.067
1742 900    Bob  2012-07-03 16:21:16.067    2012-08-03 15:56:29.897
1803 900    Bob  2012-08-03 15:56:29.897    NULL
17   900    Bob  NULL                       NULL
270  900    Bob  NULL                       2012-07-20 15:51:19.913
1780 900    Bob  2012-07-20 15:51:19.913    2012-07-26 16:26:54.413
1789 900    Bob  2012-07-26 16:26:54.413    2012-08-21 15:36:58.940
1830 900    Bob  2012-08-21 15:36:58.940    2012-08-24 14:26:05.890
1835 900    Bob  2012-08-24 14:26:05.890    2012-08-30 12:01:05.313
1838 900    Bob  2012-08-30 12:01:05.313    2012-09-05 09:29:02.497
1844 900    Bob  2012-09-05 09:29:02.497    NULL

Latest person is defined as the person with the latest (max?) Start AND (Finish IS NULL or Finish >= GetDate()) WITHIN the Group of people of same Name AND Code

In the above example that is where id = 1844 (with the groups of Bob it’s got the latest Start and the Finish is Null)

I pretty sure this is possible with a single statement but I can see how to define ‘Latest Person’ such that I can join it back to get rows I want to update

Edit: Please note that I cannot rely on the ordering of the Id column only the date columns.

  • 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-15T18:46:01+00:00Added an answer on June 15, 2026 at 6:46 pm

    Something like this will do:

    update this set code = (
       select top (1) that.code from table1 that
       where that.name = this.name -- match on name
         and (that.Finish is null or that.Finish >= getdate()) -- filter for current rows only
       order by that.Start desc, that.id desc -- rank by start, break ties with id
       )
    from table1 this
    

    I hope your table is well indexed, and/or not too big, because this is expensive to do in one step.

    Alternate form, using OUTER APPLY, and more easily extensible:

    update this set code = that.code
    from table1 this
    outer apply (
       select top (1) that.code from table1 that
       where that.name = this.name -- match on name
         and (that.Finish is null or that.Finish >= getdate()) -- filter for current rows
       order by that.Start desc, that.id desc -- rank by start, break ties with id
       ) that
    

    Alternate method using windowing functions, without a join:

    update this set code = _latest_code
    from (
      -- identify the latest code per name
      select *, _latest_code = max(
        case 
          when (finish is null or finish >= getdate()) 
           and _row_number = 1
          then code else null 
        end
        ) over (partition by name)
      from (
        -- identify the latest row per name
        select *, _row_number = row_number() over (
          partition by name order by 
            case when finish is null or finish >= getdate() then 0 else 1 end
          , start desc, id desc)
        from table1
        ) this
      ) this
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that produces a background image in a table cell.
I have the following listbox below which binds to a database table of image
I have following code for loading image from url in xml parsing endElement method
I have the following code: Image tmpimg = null; HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); HttpWebResponse
I have the following code a.rollover { background-image: url('sprite.jpg'); display: block; width: 191px; height:
i have one image background of table with following syntax style=background-image(URL) the problem is
I have the following code in my stylesheet... body { background-image:url('Background.png'); } td.Header {
Suppose I have following table in my database: Tbl_Persons: Id Country Name 1 Australia
I have the following HTML table code: <table> <tr> <th>Time</th> <th>Source</th> <th class=hide-on-phones>Destination</th> <th>Duration</th>
i have following code to create cell and add image to it UIImageView* MyImage

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.