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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:06:30+00:00 2026-05-31T17:06:30+00:00

I have the following tables: 1) TBL (ID, Data, LastUpdated, DateCreated) 2) TBL_LOG(Log_ID, LogCreateDate,

  • 0

I have the following tables:

1) TBL (ID, Data, LastUpdated, DateCreated)
2) TBL_LOG(Log_ID, LogCreateDate, ID, Data, LastUpdated, DateCreated)

TBL used to store current data and TBL_LOG used to store every change of data in TBL (using update trigger, the deleted record from TBL copied to TBL_LOG).

Now I need to retrieve the data by relevance date.
How I can write the table valued function (or other alternative) that will return the data depending on RelevanceDate.

I understand the logic, but I can’t find a good way to write it in SQL…
The logic looks like:

@RelevanceDate = '2011-03-01'
IF TBL.LastUpdated <= @RelevanceDate THEN return data from TBL
ELSE IF Exists data in TBL_LOG where TBL_LOG.LastUpdated <= @RelevanceDate 
    THEN return most resent data from TBL_LOG where TBL_LOG.LastUpdated <= @RelevanceDate
ELSE IF Exists data in TBL_LOG where TBL_LOG.LastUpdated > @RelevanceDate 
    THEN return the oldest data from TBL_LOG.LastUpdated > @RelevanceDate
ELSE return data from TBL

This function should return data using the logic above, for all records in TBL.

In short words, for each row in TBL i need data that was on “RelevanceDate”.

  • 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-31T17:06:32+00:00Added an answer on May 31, 2026 at 5:06 pm

    At the end of my investigation, i ended up with something like folowing:

    DECLARE @RelevanceDate DATETIME
    SET @RelevanceDate = '2012-03-01'
    
    SELECT 
            MainData.ID, MainData.Data, MainData.LastUpdated, MainData.DateCreated
    FROM   (
               SELECT 
                      *
                      ,ROW_NUMBER() OVER(PARTITION BY AllData.ID, AllData.RowNum ORDER BY AllData.LastUpdated) AS MainRowNumber
               FROM   (
                          --Current Data
                          SELECT 
                                 NULL LogID
                                ,NULL LogCreateDate
                                ,*
                                ,ROW_NUMBER() OVER(PARTITION BY ID ORDER BY LastUpdated DESC) AS RowNum
                          FROM   TBL
                          WHERE  (lastupdate<=@RelevanceDate)
    
                          UNION
    
                          --History Data
                          SELECT  
                                *
                                ,ROW_NUMBER() OVER(PARTITION BY ID ORDER BY LastUpdated) AS RowNum
                          FROM   TBL_LOG
                          WHERE  lastupdate>@RelevanceDate
                      ) AllData WHERE AllData.RowNum=1
           ) MainData
    WHERE  MainData.MainRowNumber = 1
    

    If someone have a suggestion how it can be done in better way, i will glad to see it 🙂

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

Sidebar

Related Questions

I have the following data on a table tbl Admission : Date and Time
I have following tables questions -> id, question_data, user_id users -> id, fname, lname
Suppose that we have following tables create table Employee( 2 EMPNO NUMBER(3), 3 ENAME
I have the following tables: users +----------+----------+----------+ | id | name | dob |
I have the following tables in my database: Products ID_PRODUCT PRODUCTNAME PRICE Customers ID_CUSTOMER
I have the following tables. I want to run a query but I think
I have the following tables. I wanted it so when I make more boxes
I have the following tables in SQL Server 2005 ReceiptPoint: ID (PK), Name GasIndexLocation:
i have the following tables in sql server: photoalbumsTable: album_ID album_caption albumtagmap id album_id
I have two tables tbl_business and business_contacts of the following structure: tbl_business --- business_id

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.