Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.server -> Re: a sql question
On 5 Mar, 22:36, "z..._at_hotmail.com" <z..._at_hotmail.com> wrote:
> Hi,
>
> Oracle 10g.
> I have a table with data like this:
>
> id val
> 1 100
> 2 101
> 3 200
> 4 300
>
> and I want a query which displays the result of "val" minus the
> "previous val", order by id, something like:
>
> id computed_value
> 2 1
> 3 99
> 4 100
>
> How do I accomplish that using sql (not pl/sql)?
>
> TIA
> Guang
SELECT t1.id, t1.val-t2.val AS computed_value
FROM tbl AS t1, tbl AS t2
WHERE t1.id-1 = t2.id;
-- David PortasReceived on Mon Mar 05 2007 - 16:53:52 CST
![]() |
![]() |