473,503 Members | 11,237 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sql - statement: building differences between data-sets

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?
Jul 19 '05 #1
2 3790
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.

ru***********@gmx.de (beyond) wrote in message news:<62**************************@posting.google. com>...
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?

Jul 19 '05 #2
ru***********@gmx.de (beyond) wrote in message news:<62**************************@posting.google. com>...
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 --
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
11104
by: avinash | last post by:
hi myself avi i am developing one appliacaion in which i am using vb 6 as front end, adodb as database library and sql sever 7 as backend. i want to update one table for which i required data from...
15
2715
by: grunar | last post by:
After some thought on what I need in a Python ORM (multiple primary keys, complex joins, case statements etc.), and after having built these libraries for other un-named languages, I decided to...
4
25570
by: Tom Walker | last post by:
I cannot get the WHERE statement to work correctly unless I use a literal with the LIKE. I want to use a working storage data name so that I can vary the WHERE statement. Example that works: ...
7
35663
by: Bob | last post by:
Currently I am using this statement to translate 3 fields in my db thru Visual Basic. I import the data from one table to another then call the IFF statements and the NewDate to translate the...
4
5593
by: Jack | last post by:
Hi, I have a asp page where part of the code is as follows. This builds up the sql statement partially. sql01 = "UPDATE EquipmentTbl SET " sql01 = sql01 & "SerialNumber = '" &...
10
1227
by: Peter Olcott | last post by:
From what I understand data alignment is not one of the things that has been standardized. If this is true, then how are libraries constructed such that one vendor's compiler can directly access...
1
21023
by: filip1150 | last post by:
I'm trying to find if there is any performance diference between explicitly using a sequence in the insert statement to generate values for a column and doing this in an insert trigger. I...
8
3266
by: gimme_this_gimme_that | last post by:
I want to execute the following SQL statement for a single emp.emp_id. Just adding and emp.emp_id=256 to the end of the statement results in a SQL statement that on average takes 5.5 seconds. ...
7
3228
by: Arun Srinivasan | last post by:
I have a problem, I am getting SQL1042N The SQL statement is not supported. error when I try to create a MQT against a nickname. The source db is db2 version 9.1 fixpack 4, same as the target db...
0
2129
by: Richard Silvers | last post by:
Hi, I'm trying to find the most efficient way to access DB2 data on z/OS DB2 V8. Any suggestions would be welcomed . . . Situation: 1. Converting a VSAM file to DB2 on z/OS 2. VSAM file...
0
7194
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.