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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:48:23+00:00 2026-06-04T00:48:23+00:00

I have two tables as follows: TableOne – RawDataId int (pk) – TimeStamp DateTime

  • 0

I have two tables as follows:

TableOne
– RawDataId int (pk)
– TimeStamp DateTime
– BuildingID int

TableTwo
– RawDataId int (pk/fk)
– MeterId int (pk)
– Value real

The MeterId is not unique and repeats multiple times (but always in equal number). The two tables join together no problem. I am able to select the top 15 rows and order the by time stamp, giving me the latest value for each meter (15 in total, each with a time stamp). However, I also need to get the Value of each meter from a previous time (exactly 1440 and 1439 minutes earlier) – if that makes any sense.

So after the query is run, I need a table with the columns from TableOne and TableTwo, but with two additional columns for ValueB and ValueC (B been the value 1440 minutes earlier, and C 1439 earlier). Ive been playing around with his all day and most of last night, and I’m slowly losing the plot.

Any help would be appreciated.
Thanks peeps.

— Update

I’ve included the actual table schema below, together with some sample data.

CREATE TABLE [dbo].[TableOne](
[RawDataId] [bigint] IDENTITY(1,1) NOT NULL,
[TimeStamp] [datetime] NOT NULL,
[BuildingId] [int] NULL,
CONSTRAINT [TableOne_PK] PRIMARY KEY CLUSTERED 

CREATE TABLE [dbo].[TableTwo](
[MeterId] [bigint] NOT NULL,
[RawDataId] [bigint] NOT NULL,
[Value] [real] NULL,
 CONSTRAINT [TableTwo_PK] PRIMARY KEY CLUSTERED 

The sample data for the last 30 records from TableOne:

RawDataId, TimeStamp, BuildingId
21677   2012-05-16 00:03:00.000 1
21678   2012-05-16 00:03:00.000 1
21679   2012-05-16 00:03:00.000 1
21680   2012-05-16 00:03:00.000 1
21681   2012-05-16 00:03:00.000 1
21682   2012-05-16 00:03:00.000 1
21683   2012-05-16 00:03:00.000 1
21684   2012-05-16 00:03:00.000 1
21685   2012-05-16 00:03:00.000 1
21686   2012-05-16 00:03:00.000 1
21687   2012-05-16 00:03:00.000 1
21688   2012-05-16 00:03:00.000 1
21689   2012-05-16 00:03:00.000 1
21690   2012-05-16 00:03:00.000 1
21691   2012-05-16 00:03:00.000 1
21662   2012-05-16 00:02:00.000 1
21663   2012-05-16 00:02:00.000 1
21664   2012-05-16 00:02:00.000 1
21665   2012-05-16 00:02:00.000 1
21666   2012-05-16 00:02:00.000 1
21667   2012-05-16 00:02:00.000 1
21668   2012-05-16 00:02:00.000 1
21669   2012-05-16 00:02:00.000 1
21670   2012-05-16 00:02:00.000 1
21671   2012-05-16 00:02:00.000 1
21672   2012-05-16 00:02:00.000 1
21673   2012-05-16 00:02:00.000 1
21674   2012-05-16 00:02:00.000 1
21675   2012-05-16 00:02:00.000 1
21676   2012-05-16 00:02:00.000 1

Sample for TableTwo:

MeterId, RawDataId, Value
15  21691   7722613
14  21690   908944
13  21689   4982947
12  21688   3821899
11  21687   6
10  21686   0
9   21685   0
8   21684   5761656
7   21683   4240048
6   21682   1541372
5   21681   283223
4   21680   1.298603E+07
3   21679   388137
2   21678   876121
1   21677   0
15  21676   7722615
14  21675   908944
13  21674   4982947
12  21673   3821899
11  21672   5
10  21671   0
9   21670   0
8   21669   5761656
7   21668   4240052
6   21667   1541372
5   21666   283223
4   21665   1.298604E+07
3   21664   388137
2   21663   876122
1   21662   0

A meter reading is written to the tables every 1 (hence the time stamp). When select the top 15 records (sorted by TimeStamp, to give me the latest values), I also need to get the values of that meter 1440 and 1439 minutes ago (relative to the latest TimeStamp). I hope this makes it clearer.

So far, my SQL query looks like this:

SELECT TOP 15 * FROM (Select TableOne.[RawDataId], 
[TimeStamp], BuildingId, MeterId, `enter code here`Value 
FROM [TableOne]
INNER JOIN TableTwo ON
TableOne = TableTwo) as PS
ORDER BY [TimeStamp];

The query gives me the follow, but I need the additional two columns with the value of the meter 1440 and 1439 minutes ago, relative to the TimeStamp:

RawDataId, TimeStamp, BuildingId, MeterId, Value
21677   2012-05-16 00:03:00.000 1   1   0
21678   2012-05-16 00:03:00.000 1   2   876121
21679   2012-05-16 00:03:00.000 1   3   388137
21680   2012-05-16 00:03:00.000 1   4   1.298603E+07
21681   2012-05-16 00:03:00.000 1   5   283223
21682   2012-05-16 00:03:00.000 1   6   1541372
21683   2012-05-16 00:03:00.000 1   7   4240048
21684   2012-05-16 00:03:00.000 1   8   5761656
21685   2012-05-16 00:03:00.000 1   9   0
21686   2012-05-16 00:03:00.000 1   10  0
21687   2012-05-16 00:03:00.000 1   11  6
21688   2012-05-16 00:03:00.000 1   12  3821899
21689   2012-05-16 00:03:00.000 1   13  4982947
21690   2012-05-16 00:03:00.000 1   14  908944
21691   2012-05-16 00:03:00.000 1   15  7722613
  • 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-04T00:48:25+00:00Added an answer on June 4, 2026 at 12:48 am

    Without seeing a data (and maybe query) sample, it’s really hard to understand the problem. If I’m understanding correctly, this should work:

    SELECT
    (
    SELECT TOP 1 Value
    FROM TableOne t1 join TableTwo t2 ON t1.RawDataId = t2.RawDataId
    WHERE t1.RawDataId IN (
    SELECT RawDataId FROM TableTwo WHERE MeterId = tbl.MeterId
    ) and TimeStamp = DATEADD(mi, -1440, tbl.TimeStamp)
    ) as ValueB,
    (
    SELECT TOP 1 Value
    FROM TableOne t1 join TableTwo t2 ON t1.RawDataId = t2.RawDataId
    WHERE t1.RawDataId IN (
    SELECT RawDataId FROM TableTwo WHERE MeterId = tbl.MeterId
    ) and TimeStamp = DATEADD(mi, -1439, tbl.TimeStamp)
    ) as ValueC
    FROM TableTwo tbl
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine that we have two tables as follows: Trades ( TradeRef INT NOT NULL,
I have two tables as follows: CREATE TABLE customer ( id INT NOT NULL
I have two tables, a destination for the update: create table dest (value int)
I have two tables, that relate via a one-to-many relationship i.e tableOne (1)----------(*) tableTwo
I have two tables as follows: Table user_prefs: user_prefs_id int(11) PK user_id int(11) item
I have two tables as follows: tblCountry (countryID, countryCode) tblProjectCountry(ProjectID, countryID) The tblCountry table
I have two tables as follows.. campaigns(campaignID, title) campaignMailList(campaignID, Sent) (The 2 tables are
I'm using PHP/MySql. If I have two tables (hypothetically) as follows: id name and
If I have two tables - Logins and Users, as follows: Logins LoginIdNo UserIdNo
I have two tables as follows: tbl_answers : id, (FK)authorID ... tbl_answer_votes : 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.