Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: How to do this trick ?

Re: How to do this trick ?

From: fumi <fumi_at_tpts5.seed.net.tw>
Date: 16 Oct 1999 16:27:34 GMT
Message-ID: <7ua91m$379$6@news.seed.net.tw>

Norazman Abu Sahir <norazman_at_ti.com> wrote in message news:38069C0B.80DF880E_at_ti.com...
> Hi,
> I have a table with one colum. When I do a select below are the data.
>
> SQL> SELECT MYNUMBER FROM TESTING;
>
> MYNUMBER
> ----------
> 1
> 3
> 5
> 10
>
> What I would like to do is how to display a different betwen a row value
> with a previous row value.
> Something like this:
>
> MYNUMBER DIFF
> ---------- -------
> 1 0
> 3 2
> 5 2
> 10 5
>
> Have any idea?
> TIA
What is your logical order of records?
If you mean the order Oracle reads from disk, this is the answer:

SQL> create table testing (mynumber number);

Table created.

SQL> insert into testing values (1);

1 row created.

SQL> insert into testing values (3);

1 row created.

SQL> insert into testing values (5);

1 row created.

SQL> insert into testing values (10);

1 row created.

SQL> select a.mynumber, nvl(a.mynumber-b.mynumber, 0) as diff   2 from (select mynumber, rownum as row_num from testing) a,   3 (select mynumber, rownum as row_num from testing) b   4 where a.row_num=b.row_num(+)+1;

 MYNUMBER DIFF
--------- ---------

        1         0
        3         2
        5         2
       10         5


If you mean the order is the column MYNUMBER, this is the answer:

SQL> select a.mynumber, nvl(a.mynumber-max(b.mynumber), 0) as diff   2 from testing a, testing b
  3 where a.mynumber>b.mynumber(+)
  4 group by a.mynumber;

 MYNUMBER DIFF
--------- ---------

        1         0
        3         2
        5         2
       10         5




Received on Sat Oct 16 1999 - 11:27:34 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US