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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:40:13+00:00 2026-06-12T03:40:13+00:00

Say you have a simple table that represents a time series for another entity

  • 0

Say you have a simple table that represents a time series for another entity identified by someID. Each row is identified by someID and a timestamp, which is not subject to any regular increment, i.e. intervals can vary:

CREATE TABLE someSeries
(
   someID int not null,
   rowTS datetime not null,
   val int not null
);
ALTER TABLE someSeries 
ADD CONSTRAINT PK_someSeries(someID, rowTS);

Is there an elegant and efficient way (so not using the cartesian product) to return all rows and display the row’s rowTS and the most recent previous rowTS for that someID?

E.g. if the data is

someID        rowTS            val
------------------------------------
1             9/1/2012         2
1             9/2/2012         3
1             9/5/2012         5
2             9/2/2012         1
2             9/4/2012         6
3             9/5/2012         7
3             9/7/2012         9
3             9/10/2012        2

That query should return

someID        rowTS            prevRowTS          val          prevVal
------------------------------------------------------------------------
1             9/1/2012         null               2            null
1             9/2/2012         9/1/2012           3            2 
1             9/5/2012         9/2/2012           5            3
2             9/2/2012         null               1            null
2             9/4/2012         9/2/2012           6            1
3             9/5/2012         null               7            null
3             9/7/2012         9/5/2012           9            7
3             9/10/2012        9/7/2012           2            9

Currently, I need something like this in my app and the way I do it is in the application tier, basically I store the last rowTS in the someID master table where it is the PK and then, upon the time series insertion, I get that value from the master table and look up the most recent previous record, and do some computation (e.g. compare val and prevVal) and insert it in the time series table.

But I was wondering if there is a fast way to do it in just SQL. The only thing that comes to mind is the cartesian product and, needless to say, that is not very efficient.

  • 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-12T03:40:15+00:00Added an answer on June 12, 2026 at 3:40 am

    Since you said that it doesn’t matter what RDBMS you are using, here is how you do that in SQL Server:

    ;WITH cte
    AS
    (
        SELECT *, ROW_NUMBER() OVER(Partition BY someID ORDER BY someID, rowTS) row_num
        FROM @Temp
    )
    SELECT c1.someID, c1.rowTS, 
      (SELECT MAX(c2.rowTS) 
       FROM cte c2 
       WHERE c2.someID = c1.someID AND c2.row_num < c1.row_num) AS prevRowTS,
      c1.val,
      (SELECT MAX(c2.val) 
       FROM cte c2 
       WHERE c2.someID = c1.someID AND c2.row_num < c1.row_num) AS prevVal
    FROM cte c1
    

    Here is a live demo

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

Sidebar

Related Questions

Say I have a simple table that contains username, firstname, lastname. How do I
Lets say I have a simple table that only contains two columns: MailingListUser -
Say I have a schema that represents a fixed-depth hierarchy like this: CREATE TABLE
Let's say I have a simple table like that: unique ID | url |
I have a simple table in Sybase, let's say it looks as follows: CREATE
Say I have simple program that emulates a board game with a number of
Let's say I have a simple table of: id,name,salary The query I have is:
Let's say I have a simple form, with a select combobox and a table
We have a simple table with say (nxm matrix) and the user will randomly
We have a simple table (an audit log) that our (3rd-party) product fills with

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.