dbTalk Databases Forums  

need query to subtract data in 2 rows

comp.databases.ibm-db2 comp.databases.ibm-db2


Discuss need query to subtract data in 2 rows in the comp.databases.ibm-db2 forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
siva perumal
 
Posts: n/a

Default need query to subtract data in 2 rows - 10-05-2010 , 06:09 AM






Hour Reading Data
8AM 100
9AM 500
10AM 700
11AM 900

I need a query to come up with the trend reading data in every 1 hr
like below

Time Trend
8 - 9 AM 500-100=400
9 - 10 AM 700-500=200

Reply With Quote
  #2  
Old   
globomike
 
Posts: n/a

Default Re: need query to subtract data in 2 rows - 10-05-2010 , 06:40 AM






On 5 Okt., 13:09, siva perumal <srivi.s... (AT) gmail (DOT) com> wrote:
Quote:
Hour * * * * * * * * * * * * * *Reading Data
8AM * * * * * * * * * * * * * *100
9AM * * * * * * * * * * * * * *500
10AM * * * * * * * * * * * * *700
11AM * * * * * * * * * * * * *900

I need a query to come up with the trend reading data in every 1 hr
like below

Time * * * * * * * * * * * *Trend
8 - 9 AM * * * * * * * * * 500-100=400
9 - 10 AM * * * * * * * * 700-500=200
Hi Siva

check out the DB2 OLAP functions.
"LAG" for example allows you to work with the content of two
succeeding rows.

Have look into the DB2 Cookbook ( http://mysite.verizon.net/Graeme_Birchall/id1.html
)
for more details and sample.

kind regards
Michael

Reply With Quote
  #3  
Old   
Frederik Engelen
 
Posts: n/a

Default Re: need query to subtract data in 2 rows - 10-05-2010 , 07:03 AM



On Oct 5, 1:40*pm, globomike <M_Tiefenbac... (AT) gmx (DOT) de> wrote:
Quote:
On 5 Okt., 13:09, siva perumal <srivi.s... (AT) gmail (DOT) com> wrote:

Hour * * * * * * * * * * * * * *Reading Data
8AM * * * * * * * * * * * * * *100
9AM * * * * * * * * * * * * * *500
10AM * * * * * * * * * * * * *700
11AM * * * * * * * * * * * * *900

I need a query to come up with the trend reading data in every 1 hr
like below

Time * * * * * * * * * * * *Trend
8 - 9 AM * * * * * * * * * 500-100=400
9 - 10 AM * * * * * * * * 700-500=200

Hi Siva

check out the DB2 OLAP functions.
"LAG" for example allows you to work with the content of two
succeeding rows.

Have look into the DB2 Cookbook (http://mysite.verizon.net/Graeme_Birchall/id1.html
)
for more details and sample.

kind regards
Michael
Hi,

Without using OLAP functions, it's also possible by joining this table
with itself (although OLAP might be more performant):

select b.data - a.val
from tab a join tab b on a.time = b.time -1

--
Frederik Engelen

Reply With Quote
  #4  
Old   
ChrisC
 
Posts: n/a

Default Re: need query to subtract data in 2 rows - 10-05-2010 , 10:47 AM



If there is a lot of data and you do not have the right indexes (on
time), then OLAP will be significantly more performant.

Also, OLAP can handle missing hours/day boundaries better, depending
on how the data is stored. Check out the rows/range options for what
works best in your case.

-Chris

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.