dbTalk Databases Forums  

sql - statement: building differences between data-sets

comp.databases.oracle comp.databases.oracle


Discuss sql - statement: building differences between data-sets in the comp.databases.oracle forum.



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

Default sql - statement: building differences between data-sets - 08-03-2004 , 03:17 PM






i need a sql-statement for this problem

i ve got a table like this
ID;value
1;30
2;34
3;44

the result of sql-statement should calculate differences to
previous/other datasets like this
ID;diff
1; 30-0
2; 34-30
3; 44-34

who can solve this problem elegant?

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

Default Re: sql - statement: building differences between data-sets - 08-04-2004 , 02:14 AM






Try Analytic functions.

(As Tom Kyte would say: 'Analytics Rock').

An alternative is a join like this:

select 2.id ID, (2.value - nvl(1.value,0)) diff
from table 1, table 2
where 2.id - 1 = 1.id (+)

Cheers.

Carlos.

rudi.ramstein (AT) gmx (DOT) de (beyond) wrote in message news:<621ad8ac.0408031217.41fc6fc3 (AT) posting (DOT) google.com>...
Quote:
i need a sql-statement for this problem

i ve got a table like this
ID;value
1;30
2;34
3;44

the result of sql-statement should calculate differences to
previous/other datasets like this
ID;diff
1; 30-0
2; 34-30
3; 44-34

who can solve this problem elegant?

Reply With Quote
  #3  
Old   
Mark D Powell
 
Posts: n/a

Default Re: sql - statement: building differences between data-sets - 08-04-2004 , 09:13 AM



rudi.ramstein (AT) gmx (DOT) de (beyond) wrote in message news:<621ad8ac.0408031217.41fc6fc3 (AT) posting (DOT) google.com>...
Quote:
i need a sql-statement for this problem

i ve got a table like this
ID;value
1;30
2;34
3;44

the result of sql-statement should calculate differences to
previous/other datasets like this
ID;diff
1; 30-0
2; 34-30
3; 44-34

who can solve this problem elegant?
When a newsgroup has subgroups you should generally post to the
subgroups (.server, .misc, .tools, and .marketplace) rather than to
the group.

Join the table to itself on the key value - 1

select a.id, a.value, b.id, b.value, nvl(b.value,0) - nvl(a.value,0)
as Diff
from marktest3 a,
(select (c.id - 1) AS id, c.value
from marktest3 c
) b
where a.id(+) = b.id

ID VALUE ID VALUE DIFF
---------- ---------- ---------- ---------- ----------
0 30 30
1 30 1 34 4
2 34 2 44 10

There are other ways to accomplish the same output.

HTH -- Mark D Powell --


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.