I created a sequence
CREATE SEQUENCE name_seq
START WITH 1
INCREMENT BY 1
NOCACHE
NOCYCLE;
Then used it in my query:
select Name, ROWNUM, name_seq.NEXTVAL
from myTable;
Results into something like this:
+----+------+-------+
|Name|ROWNUM|NEXTVAL|
+----+------+-------+
|A | 1| 1|
|B | 2| 2|
|C | 3| 3|
|D | 4| 4|
|E | 5| 5|
|F | 6| 6|
|G | 7| 7|
|H | 8| 8|
|I | 9| 9|
|J | 10| 10|
+----+------+-------+
And from this, what I want the output to be is to use the next value of my sequence every after a number that I will declare. For example I'll use 4. The output should be like this:
In 1st run:
+----+------+-------+
|Name|ROWNUM|NEXTVAL|
+----+------+-------+
|A | 1| 1|
|B | 2| 1|
|C | 3| 1|
|D | 4| 1|
|E | 5| 2|
|F | 6| 2|
|G | 7| 2|
|H | 8| 2|
|I | 9| 3|
|J | 10| 3|
+----+------+-------+
Then in 2nd run:
+----+------+-------+
|Name|ROWNUM|NEXTVAL|
+----+------+-------+
|A | 1| 4|
|B | 2| 4|
|C | 3| 4|
|D | 4| 4|
|E | 5| 5|
|F | 6| 5|
|G | 7| 5|
|H | 8| 5|
|I | 9| 6|
|J | 10| 6|
+----+------+-------+
try this,
CREATE SEQUENCE name_test_seq
START WITH 1
INCREMENT BY 1
NOCACHE
NOCYCLE;
CREATE OR REPLACE FUNCTION get_next_val (p_num NUMBER) RETURN NUMBER
IS
BEGIN
IF p_num = 1 THEN
RETURN name_test_seq.NEXTVAL;
ELSE
RETURN name_test_seq.CURRVAL;
END IF;
END;
/
select rownum rown, get_next_val(MOD(rownum, :p_num))
from dual
connect by rownum <= 10
Try this :
Select name,rownum,floor((name_seq.NEXTVAL-1)/4)+1 fl
from myTable
Just substract the sequence last number.
with seq_cur_val as(
select last_number seq_cv
from user_sequences
where sequence_name='NAME_SEQ'
)
select name, rownum,
floor((name_seq.nextval-seq_cv)/4)+1
from myTable,seq_cur_val;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.