Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: giving tuples a number to make them unique..
On Thu, 14 Jan 1999 15:29:46 GMT, rosinowski_at_gmx.de (Jan Rosinowski)
wrote:
>
>i need to number rows in a _select_-statement to get unique tuples
>with ascending instanceids:
>
>name, instanceid
>a, 1
>a, 2
>b, 1
>c, 1
>c, 2
>c, 3
>d, 3
>
>(the select-statement is a flattened inclusion-hierarchy using
>connect-by which is intended to be inserted in another relation)
>
>any ideas?
You could solve it with a function.
eg.
SQL> desc t
Name Null? Type ------------------------------- -------- ---- A VARCHAR2(1)
SQL> select * from t;
A
-
a
a
b
c
c
c
d
7 rows selected.
create or replace
package my_seq as
function inst_id( p_a varchar2 ) return number;
pragma restrict_references( inst_id, WNDS );
end my_seq;
/
create or replace
package body my_seq as
g_cnt number;
g_value varchar2(1);
function inst_id( p_a varchar2 ) return number is
begin
if g_value <> p_a or g_value is null then
dbms_output.put_line( p_a );
g_cnt := 0; g_value := p_a;
begin
g_cnt := 0;
g_value := null;
end my_seq;
/
SQL> select a name, my_seq.inst_id(a) instanceid from t order by a;
NAME INSTANCEID
---------- ----------
a 1 a 2 b 1 c 1 c 2 c 3 d 1
7 rows selected.
Hope this helps.
chris.
>
>if that's not possible: how can i get a sequence on the fly? in sybase
>one uses number(*) for that purpose..
>
>ciao, jan
--
Christopher Beck
Oracle Corporation
clbeck_at_us.oracle.com
Reston, VA.