Saturday, January 28, 2017

SQL101: Using sequence.nextval with group and analytical function.

In SQL101 series, I will post some simple SQL related techniques. Most of them are posted in OTN time to time by me.
This SQL101 is related to Using sequence.nextval with group and analytical function, the problem was posted in OTN.
The problem with sequence.nextval is it increments within SQL statement even within the group. Carefully watch this test case:
SCOTT@orclSB 28-JAN-17> --My database version.
SCOTT@orclSB 28-JAN-17> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE    11.2.0.1.0      Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production

SCOTT@orclSB 28-JAN-17> ed
Wrote file afiedt.buf

  1  --Creating the sequence.
  2* create sequence file_seq minvalue 1 maxvalue 9999999999999999999999999999 increment by 1 start with 100 cache 20 noorder nocycle
SCOTT@orclSB 28-JAN-17> /

Sequence created.

SCOTT@orclSB 28-JAN-17> ed
Wrote file afiedt.buf

  1  --Creating the test table.
  2  create table test4
  3    ( file_number number, seq_no number
  4*   )
SCOTT@orclSB 28-JAN-17> /

Table created.

SCOTT@orclSB 28-JAN-17> ed
Wrote file afiedt.buf

  1  --Inserting my test data.
  2  insert into test4
  3  select 10,null from dual
  4  union all
  5  select 10,null from dual
  6  union all
  7  select 10,null from dual
  8  union all
  9  select 20,null from dual
 10  union all
 11  select 20,null from dual
 12  union all
 13* select 30,null from dual
SCOTT@orclSB 28-JAN-17> /

6 rows created.

SCOTT@orclSB 28-JAN-17> commit;

Commit complete.

SCOTT@orclSB 28-JAN-17> select file_number,dense_rank() over (order by file_number) dnk from test4;

FILE_NUMBER        DNK
----------- ----------
         10          1
         10          1
         10          1
         20          2
         20          2
         30          3

6 rows selected.

SCOTT@orclSB 28-JAN-17> 
 
 
Required Output
FILE_NUMBER, DNK
10                          100
10                          100
10                          100
20                          101
20                          101
30                          103

Rules:
If you noticed that I created sequence name as file_SEQ, system should  user this sequence to generate dnk column  and if file_number change then get next sequence. So far I created the below query but it is not working.
SCOTT@orclSB 28-JAN-17> ed
Wrote file afiedt.buf

  1  select file_number,
  2    dense_rank() over (order by file_number) + file_seq.nextval dnk
  3* from test4
SCOTT@orclSB 28-JAN-17> /

FILE_NUMBER        DNK
----------- ----------
         10        101
         10        102
         10        103
         20        105
         20        106
         30        108

6 rows selected.

SCOTT@orclSB 28-JAN-17> 
Now, you can see the problem, it is creating counting without grouping of file_number.
This is the actual solution of this problem:

SCOTT@orclSB 28-JAN-17> ed
Wrote file afiedt.buf

  1  create or replace function retfileseq(
  2      pi_indicator integer)
  3    return number
  4  as
  5  begin
  6    if pi_indicator = 1 then
  7      return (file_seq.nextval);
  8    else
  9      return (file_seq.currval);
 10    end if;
 11* end retfileseq;
SCOTT@orclSB 28-JAN-17> /

Function created.

SCOTT@orclSB 28-JAN-17> ed
Wrote file afiedt.buf

  1  select file_number ,
  2    case
  3      when lag(file_number) over(order by file_number) is null
  4      or file_number <> lag(file_number) over(order by file_number)
  5      then retfileseq(1)
  6      else retfileseq(0)
  7    end as file_seq
  8* from test4
SCOTT@orclSB 28-JAN-17> /

FILE_NUMBER   FILE_SEQ
----------- ----------
         10        106
         10        106
         10        106
         20        107
         20        107
         30        108

6 rows selected.

SCOTT@orclSB 28-JAN-17> 

No comments :

Post a Comment