Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Get the date of a previous date in the same column without a set interval.

$
0
0

I have looked for an answer and maybe I missed it but I cannot find what I need.

I have a table that I need to get the pervious date that a record was added for aspecific item, here is a sample of my table:

ID  BldgID         MeterNumber   ReadDate       MeterReading    SuiteNumber    Multiplier     kWpH    UserID
1    0123           225588            2014-01-01    19947              NULL                  1              0.0665   1
2    0123           225588            2014-02-02    20947              NULL                  1              0.0665   1
3    0123           225588            2014-03-10    21535              NULL                  1              0.0665   1
4    112233       556655            2014-01-01    568535             NULL                 10             0.0665   1
5    112233       556655            2014-01-30    569524             NULL                 10            0.0665   1

So I need to output a report that shows something like:

                             Jan-2014                                         Feb-2014                                              etc..

Building   Meter      Previous Reading - Current Reading   Previous Reading - Current Reading

0123       225588    N/A                       19947                  19947                   20947

.

.

.

112233    556655    N/A                       568535               568535                  569524            

While this is simplified somewhat it explains what I am looking for, so to summarize I need the "previous reading" of each item to show the item may have many entries and they are not at set intervals. The report will run on a date range set by the user so there is no set start and finish dates, these will be variables.

Here is one of the things I tried:

SELECTMeterReading
FROM SubMeters
WHERE ReadDate BETWEEN dateadd(m,datediff(m,0,getdate())-1,0) and dateadd(m,datediff(m,0,ReadDate),0)

Any ideas or help would be greatly appreciated, thank you.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>