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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:11:04+00:00 2026-06-16T19:11:04+00:00

I have following code SELECT MRT.sno, MRT.TypeName, MR.Adate, MAX(MRD.Value) AS Value FROM MeterReadings MR

  • 0

I have following code

SELECT MRT.sno, MRT.TypeName, MR.Adate, MAX(MRD.Value) AS Value
FROM MeterReadings MR 
INNER JOIN MeterReadingDetails MRD ON MRD.ReadingId = MR.sno
INNER JOIN MeterReadingTypes MRT ON MRT.sno = MRD.ReadingTypeId
WHERE MRT.sno IN (7,10,11)
GROUP BY MRT.sno,MRT.TypeName,MR.Adate
ORDER BY MR.Adate DESC

And result

sno TypeName                Adate                   Value

11  Toplam Kapasitif        2013-01-04 00:00:00     33,313
7   Toplam                  2013-01-04 00:00:00     7819,33
10  Toplam Reaktif          2013-01-04 00:00:00     640,492
11  Toplam Kapasitif        2013-01-03 00:00:00     33,276
7   Toplam                  2013-01-03 00:00:00     7805,934
10  Toplam Reaktif          2013-01-03 00:00:00     639,862

I want an additional column that named “OldValue”. OldValue Column shows previous day value like following (last three row for above example)

33,276
7805,934
639,862
null
null
null

How Can I do this? There may be a similar example to show me the way.

Thanks in advance…

UPDATE

In fact, I wrote something like this

SELECT MRT.sno, MRT.TypeName, MR.Adate, MAX(MRD.Value) AS Value, 

(SELECT Value From
    (SELECT TOP 1 XMR.Adate,XMRD.ReadingTypeId,MAX(XMRD.Value) AS Value 
    FROM MeterReadings XMR,MeterReadingDetails XMRD 
    WHERE XMRD.ReadingId = XMR.sno AND XMRD.ReadingTypeId = MRT.sno AND XMR.Adate<MR.Adate 
    GROUP BY XMR.Adate,XMRD.ReadingTypeId 
    ORDER BY XMR.Adate DESC) AS TBL
) AS OldValue

FROM MeterReadings MR 
INNER JOIN MeterReadingDetails MRD ON MRD.ReadingId = MR.sno
INNER JOIN MeterReadingTypes MRT ON MRT.sno = MRD.ReadingTypeId
WHERE MRT.sno IN (7,10,11)
GROUP BY MRT.sno,MRT.TypeName,MR.Adate

But I dont know, Is this best way?

  • 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-16T19:11:05+00:00Added an answer on June 16, 2026 at 7:11 pm

    If you’r using SQL Server 2012, you could use the new LAG analytical function

    SQL Server 2012 introduces new analytical function LEAD() and LAG().
    This functions accesses data from a subsequent row (for lead) and
    previous row (for lag) in the same result set without the use of a
    self-join .

    Your statement then becomes

    SELECT  *, LAG(Value) OVER (PARTITION BY sno ORDER BY Adate DESC)
    FROM    (
              SELECT  MRT.sno, MRT.TypeName, MR.Adate, MAX(MRD.Value) AS Value, 
              FROM    MeterReadings MR 
                      INNER JOIN MeterReadingDetails MRD ON MRD.ReadingId = MR.sno
                      INNER JOIN MeterReadingTypes MRT ON MRT.sno = MRD.ReadingTypeId
              WHERE   MRT.sno IN (7,10,11)
              GROUP BY
                      MRT.sno,MRT.TypeName,MR.Adate
            ) q
    ORDER BY
            Adate DESC
    

    Using SQL Server 2005/2008, I would write the statement as

    ;WITH q AS (
      SELECT  MRT.sno, MRT.TypeName, MR.Adate, MAX(MRD.Value) AS Value, rn = ROW_NUMBER() OVER (PARTITION BY MRT.sno ORDER BY MR.Adate DESC)
      FROM    MeterReadings MR 
              INNER JOIN MeterReadingDetails MRD ON MRD.ReadingId = MR.sno
              INNER JOIN MeterReadingTypes MRT ON MRT.sno = MRD.ReadingTypeId
      WHERE   MRT.sno IN (7,10,11)
      GROUP BY
              MRT.sno,MRT.TypeName,MR.Adate
    )
    SELECT  q1.*, q2.Value
    FROM    q q1
            LEFT OUTER JOIN q q2 ON q2.sno = q1.sno AND q2.rn = q1.rn + 1
    

    Edit

    The main difference between using either one of these solutions to yours is that your solution has to retrieve the previous result for each record, an expensive operation, while this solutions in essence can join two complete (identical) sets of data.

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

Sidebar

Related Questions

I have the following SQL code: select val.PersonNo, val.event_time, clg.number_dialed from vicidial_agent_log val join
I have following MySQL Code: SELECT * FROM b_movies, b_mov_rol_celeb, b_starcast LEFT JOIN bb_celebs
Assuming that I have the following T-SQL code: SELECT * FROM Foo f INNER
I Have following code with LINQ var q = (from web in DataContext.Webs select
I have the following code: function isValidAuthor($authorID){ $query = SELECT * FROM jos_users WHERE
I have the following code <select id=practice_id name=practice column=practice style=width: 200px> <option value=>Default</option> <option
I have the following code in html: <select> <option text=item9 value=9></option> <option text=item10 value=10></option>
I have the following code $result = $handle->select()->from('store_details') ->where('store_details.store_id=?', $id) ->columns('store_details.store_name'); //->query(ZEND_DB::FETCH_OBJ); However, when
I have the following SQL code: select @colNames = coalesce(@colNames+',','')+''''+ RptLOB1 +'''' from CM.Correlation
I have the following code: $sql = SELECT name, address, city FROM tableA, tableB

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.