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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:36:39+00:00 2026-05-30T08:36:39+00:00

I have the following file name changing log table. ChangeNameLog( Date , OldName ,

  • 0

I have the following file name changing log table.

ChangeNameLog(Date, OldName, NewName)
Primary key: Date, OldName

The data of the table looks like

Date OldName NewName 
1/1  aaa     bbb
1/2  bbb     ccc
1/3  ccc     bbb
1/4  bbb     ddd
2/1  xx      yy
2/2  yy      zz

(The file name aaa was changed to bbb, and then to ccc, bbb, ddd later
The file name xx was changed to yy, and then to zz)

I want to get the oldest name for all the new names. The result will look like

Date NewName OldestName
1/2  bbb     aaa
1/3  ccc     aaa
1/4  ddd     aaa
2/1  yy      xx
2/2  zz      xx

Is anyway to write a Transact-SQL (Version 2008 is fine) without using cursor to loop the logging table?

The following SQL can be used to prepare the data.

declare @log table (
    Date Date, OldName varchar(20), NewName varchar(20) not null 
    primary key (Date, OldName)
);
-- The real table also have the following CK
-- create unique index IX_CK on @log (Date, NewName)

insert into @log values
 ('2012-01-01', 'aaa', 'bbb')
,('2012-01-02', 'bbb', 'ccc')
,('2012-01-03', 'ccc', 'bbb')
,('2012-01-04', 'bbb', 'ddd')
,('2012-01-05', 'ddd', 'eee')

,('2012-01-03', 'xx',  'yy')
,('2012-02-02', 'yy',  'zz')
,('2012-02-03', 'zz',  'xx')
;
  • 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-30T08:36:40+00:00Added an answer on May 30, 2026 at 8:36 am

    My own solution:

    declare @log table (
        Date Date, OldName varchar(20), NewName varchar(20) 
        primary key (Date, OldName)
    );
    
    insert into @log values
     ('2012-01-01', 'aaa', 'bbb')
    ,('2012-01-02', 'bbb', 'ccc')
    ,('2012-01-03', 'ccc', 'bbb')
    ,('2012-01-04', 'bbb', 'ddd')
    ,('2012-01-05', 'ddd', 'eee')
    
    ,('2012-01-03', 'xx',  'yy')
    ,('2012-02-02', 'yy',  'zz')
    ,('2012-02-03', 'zz',  'xx')
    ;
    
    ;with m as (
        select Date, OldName, NewName, 1 as L
        from @log 
        union all
        select l.Date, m.OldName, l.NewName, L + 1
        from @log l join m on l.Date > m.Date and l.OldName = m.NewName
    )
    select * 
    from m
    where L = (select MAX(l) from m m1 where NewName = m.NewName and Date = m.Date)
    order by 1
    

    Output:
    The following result shows that the two original names are aaa and xx.

    Date       Orig Name L
    2012-01-01  aaa bbb 1
    2012-01-02  aaa ccc 2
    2012-01-03  aaa bbb 3
    2012-01-03  xx  yy  1
    2012-01-04  aaa ddd 4
    2012-01-05  aaa eee 5
    2012-02-02  xx  zz  2
    2012-02-03  xx  xx  3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following variable that accepts a file name: var xtr = new
I have the following lines in my ~/.emacs.d/init.el (custom-set-variables '(flymake-allowed-file-name-masks (quote ( (\\.cc\\' flymake-simple-make-init)
I have written a following code to get just the file name without extension
I have an .erb file where I'd like to return the following text: Name
Hi, I have a input that looks like this : <input type=file id=ModelViewAd.Files[0] name=ModelViewAd.Files[0]
I have the following code, which will retrieve a filename from a table and
Say I have a database table like the following: FileID | FileName | FileSize
I have a filename that has the following format: timestamp-username-1 This file is constantly
I have been mocking out some seam components using the following signature: @Name(myService) @Install(debug
I have the following basic code to read a text file from a StreamReader:

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.