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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:20:04+00:00 2026-05-27T10:20:04+00:00

I’m working with a transactional database: a relational database where a row in a

  • 0

I’m working with a transactional database: a relational database where a row in a table is defined as the set of values the database, at a given time, “thinks” the row’s fields contain.

Part of the schema:

CREATE TABLE Article
(
    id INTEGER,
    PRIMARY KEY(id)
);

CREATE TABLE Article_headline
(
    Article_id INTEGER,
    headline TEXT,
    tt DATETIME
    FOREIGN KEY(Article_id) REFERENCES Article(id)
);

CREATE TABLE Article_body
(
    Article_id INTEGER,
    body TEXT,
    tt DATETIME
    FOREIGN KEY(Article_id) REFERENCES Article(id)
);

In other words, the virtual “Article” table has three columns: id, headline, and body.

I can create temporary views to represent the virtual “Article” table defined by a given time:

CREATE TEMPORARY VIEW Article_headline_old
AS SELECT Article_id, headline, MAX(tt)
FROM Article_headline
WHERE tt <= "2011-11-03 16:05:23"
GROUP BY Article_id;

CREATE TEMPORARY VIEW Article_body_old
AS SELECT Article_id, body, MAX(tt)
FROM Article_body
WHERE tt <= "2011-11-03 16:05:23"
GROUP BY Article_id;

CREATE TEMPORARY VIEW Article_old
AS SELECT id, headline, body
FROM Article, Article_headline_old, Article_body_old
WHERE Article_headline_old.Article_id = Article.id
AND Article_body_old.Article_id = Article.id;

I have to create these three temporary views each time I want to query the database to get an article, which I would rather not do. I would prefer to create permanent view “functions” as part of the schema itself:

CREATE VIEW Article_headline_at(theTime)
AS SELECT Article_id, headline, MAX(tt)
FROM Article_headline
WHERE tt <= theTime
GROUP BY Article_id;

CREATE VIEW Article_body_at(theTime)
AS SELECT Article_id, body, MAX(tt)
FROM Article_body
WHERE tt <= theTime
GROUP BY Article_id;

CREATE VIEW Article_at(theTime)
AS SELECT id, headline, body
FROM Article,
     Article_headline_at(theTime) AS Article_headline_old,
     Article_body_at(theTime) AS Article_body_old
WHERE Article_headline_old.Article_id = Article.id
AND Article_body_old.Article_id = Article.id;

And then when I want to select an article from the database:

SELECT * FROM Article_at("2011-11-03 16:05:23");

Does SQL have any similar functionality?

  • 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-27T10:20:05+00:00Added an answer on May 27, 2026 at 10:20 am

    You could create three stored procedures and pass theTime as a variable to each.

    For Article_headline_at and Article_body_old_at insert these stored procedures into a temporary table inside Article_at stored procedures and do the query. This could be your solution:

    CREATE PROCEDURE Article_headline_at @theTime datetime as
    
    SELECT Article_id, headline, MAX(tt)
    FROM Article_headline
    WHERE tt <= theTime
    GROUP BY Article_id;
    
    -----
    
    
    
    CREATE PROCEDURE Article_body_old_at @theTime datetime as
    SELECT Article_id, body, MAX(tt)
    FROM Article_body
    WHERE tt <= theTime
    GROUP BY Article_id;
    
    -----
    
    CREATE PROCEDURE Article_at @theTime datetime as
    IF OBJECT_ID('tempdb.dbo.#Article_headline_at') IS NOT NULL DROP TABLE tempdb.dbo.#Article_headline_at
    IF OBJECT_ID('tempdb.dbo.#Article_body_old_at') IS NOT NULL DROP TABLE tempdb.dbo.#Article_body_old_at
    
    create table #Article_headline_at (
    Article_id int,
    body nvarchar(max),
    tt int
    )
    
    create table #Article_body_old_at (
    Article_id int,
    headline nvarchar(max),
    tt int
    )
    
    declare @sql1 nvarchar(max)
    declare @sql2 nvarchar(max)
    
    set @sql1 = 'insert into #Article_headline_at exec #Article_headline_at @theTime = '''+ @theTime + ''''
    set @sql2 = 'insert into #Article_body_old_at exec #Article_body_old_at @theTime = '''+ @theTime + ''''
    
    exec sp_executesql @sql1
    exec sp_executesql @sql2
    
    
    SELECT id, headline, body
    FROM Article, #Article_headline_at(theTime), #Article_body_at(theTime)
    WHERE #Article_headline_old.Article_id = Article.id
    AND #Article_body_old.Article_id = Article.id;
    
    -----
    

    so at the end of it all you can do

    exec Article_at @theTime = “2011-11-03 16:05:23”

    but I am sure there is a easier way around but this the only way I can think of.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a reasonable size flat file database of text documents mostly saved in
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.