Reg: NTILE function-

Hi Experts,

I was going through the functions in the Oracle docs I've ever used, to learn something new.

Had a doubt - what is real and practical function usage - NTILE ?

I have tried to play with it and understand it's job but a practical example will give a better understanding.

In addition, another curious question - what may be the scenario where, after having distributed any amount also lines 'x' buckets, lines of the rest are distributed 1 each in series in all of the buckets?

RANIT@XE11GR2 > > select * from x 1;

COL1 COL2 COL3 COL4

---------- ---------- ------------------------------ --------------------------------------------------

1401 1 a1 aa1

2        102 a2                             aa2

3        103 a3                             aa3

4        104 a4                             aa4

5        105 a5                             aa5

6        101 a1                             aa1

7        107 a7                             aa7

8        101 a1                             aa1

9        109 a9                             aa9

10 102 a2, aa2

A1 11 101-aa1

A1 12 101-aa1

12 selected lines.

Elapsed time: 00:00:00.14


RANIT@XE11GR2 > > select col1, col2, col3, col4,.

2 NTILE (5) over (order by col2) nt

3 x 1;


COL1 COL2 COL3 COL4 NT

---------- ---------- ----- ------ ----------

6 101 aa1 a1 1

11 101 aa1 a1 1

8 101 aa1 a1 1

12 101 aa1 a1 2

10 102 a2 AA 2 2

2 102 a2 AA 2 2

3 103 a3 aa3 3

4 104 a4 aa4 3

5 105 aa5 a5 4

7 107 a7 aa7 4

9 109 a9 aa9 5

1401 1 aa1 a1 5

12 selected lines.

Any help in this regard is highly appreciated.

Concerning

-Nordine

(on Oracle version 11.2.0.2.0)

nordine B wrote:

My doubt can be the possible business requirements where NTILE can be used. Can you give a business case?

NTILE is obviously not of great importance, but, for example, you can assign the number of ordered parcel items not knowing that N items can fit in a single packet. Or you can use it in statistical calculations - divide all the measures into N tiles and consider tiles first K and the last sound. Or you just want to divide the work between the processes of N.

SY.

Tags: Database

Similar Questions

  • NTILE function

    After using the analytical function, NTILE, I started to use it to divide my lines in batches. However, I would like to convey our expressions dynamically (depending on the number of lines), but unfortunately, I see some limitations prevent me to do this. The reason why I want to do this dynamically is when there are lines < = value of the expression, NTILE function forms a lot by each record that I do not want to happen.

    If you notice in the example below, it creates 10 batches/buckets when there are only 10 lines, where I wish to have 1 single lot.
    SQL> drop table r_dummy;
    
    Table dropped.
    
    SQL>
    SQL> create table r_dummy
      2  as
      3  select level id
      4  from dual
      5  connect by level<=10;
    
    Table created.
    
    SQL>
    SQL> select ntile(10) over(order by id) batch_no,id
      2  from r_dummy;
    
      BATCH_NO         ID
    ---------- ----------
             1          1
             2          2
             3          3
             4          4
             5          5
             6          6
             7          7
             8          8
             9          9
            10         10
    
    10 rows selected.
    
    SQL>
    SQL> select ceil(count(*)/10) from r_dummy;
    
    CEIL(COUNT(*)/10)
    -----------------
                    1
    
    SQL>
    I want to use above even ceil function in my real application but
    with t as
    (
    select id, ceil((count(*) over())/10) bucket_size
    from r_dummy
    )
    select id,ntile(bucket_size) over(order by id) batch_no
    from t;
    
    ERROR at line 6:
    ORA-30488: argument should be a function of expressions in PARTITION BY
    I had seen the same question asked by Sven Weller [url https://forums.oracle.com/forums/thread.jspa?threadID=587782] here, would like to know if there is no solution available for this workaround.

    Please help me.

    http://docs.Oracle.com/CD/B19306_01/server.102/b14200/functions101.htm

    Value expr (bucket_size) must correspond to a positive constant for each partition.

    with t as
    (
    select id, ceil((count(*) over())/10)  bucket_size
    from r_dummy
    )
    select id,ntile(bucket_size) over(partition by bucket_size order by id) batch_no
    from t;
    
            ID   BATCH_NO
    ---------- ----------
             1          1
             2          1
             3          1
             4          1
             5          1
             6          1
             7          1
             8          1
             9          1
            10          1
    
    10 rows selected.
    
    with t as
    (
    select id, ceil((count(*) over())/5) bucket_size
    from r_dummy
    )
    select id,ntile(bucket_size) over(partition by bucket_size order by id) batch_no
    from t;
    
            ID   BATCH_NO
    ---------- ----------
             1          1
             2          1
             3          1
             4          1
             5          1
             6          2
             7          2
             8          2
             9          2
            10          2
    
    10 rows selected.
    
  • Reg: Count function call-

    Hi Experts,

    I am tracking my processing batch every night and find a particular function called many times.

    Is it possible that I can find how many times it was performed in a given period of time?

    AWR? Or, any dynamic view v$ _?

    Please notify.

    Thank you and best regards,

    -Nordine

    (on Oracle 10.2.0.4.0)

    Asked me the same question to the company less than a month ago so I can shed some light on this particular situation (not too much).

    Some (say seven) years ago our thinkers came up with the idea of VAE and apparently I was the only one who objected to the approach (https://www.simple-talk.com/opinion/opinion-pieces/bad-carma/ and articles in several Tom Kyte has had no effect he was only harmful for me - no promotion,...).

    Therefore, the number of attributes is greatly increased and the situation becomes more and more unbearable.

    As it is mandatory to call for attribute values, it is not just online treatment, plans sql also suffer (calling functions for each line) that the same functions are called from sql too.

    Verification of AWR reports it turns out that some functions that provide attribute values are called more than a hundead million times a day, and as we call them around there is little use querrying dependencies without taking into account the volumes of data (if only someone knew how).

    So something like WHO_CALLED_ME is required to identify the heaviest appellants and (much more than that), it is a problem of how silently (as much as possible) reduce the number of attributes where nobody is now willing to admit the decision was wrong or (not even in my dreams) that I was right.

    As far as I KNOW two attributes have been removed these time and proclaimed a spectacular feat of tuning.

    Chi lives, vedra that lets say our Italian neighbours.

    Concerning

    Etbin

  • F3 function key does not work does not in reg edit

    When REG change function key F # does not work to find the order of the day.

    Hello

    Maybe it's down to how the function keys are defined in the Bios - if you hold down the fn key and then press f3 does this work?

    Kind regards

    DP - K

  • analytical function, NTILE - need help

    Hi all

    I have an obligation to distribute the workload in different threads 4 equal but from the weightage (volume) that is I want son to have a charge equal in terms of number and volume.

    I created following example and using the ntile function to spread the load in buckets.

    SQL > select id, val
    2 of jit_weightage
    3 val desc order;

    VAL ID
    ---------- ----------
    2          7
    1          6
    4          6
    3          4
    7          1
    5          1
    8          1
    6          1

    8 selected lines.

    SQL > select id, val, NTILE (4) OVER (ORDER BY val DESC) bucket
    2 of jit_weightage;

    BUCKET OF VAL ID
    ---------- ---------- ----------
    2          7          1
    1          6          1
    4          6          2
    3          4          2
    7          1          3
    5          1          3
    8          1          4
    6          1          4

    8 selected lines.

    He distributed the County in the buckets also but it does not solve my problem as thread 1 will choose larger volume ID so ideally I want output like below, so that each thread will have the same counts and a charge equal to work on. Could you help me write a query for such release.

    BUCKET OF VAL ID
    ---------- ---------- ----------
    2          7          1
    1          6          2
    4          6          3
    3          4          4
    7          1          1
    5          1          2
    8          1          3
    6          1          4

    8 selected lines.

    Kind regards

    JM

    This will give you equal counties while spreading the volume a bit also. It works like the draft: first round resumes in a single order, second round resumes in reverse.

    drop table t cascade constraints purge;
    create table t(ID, VAL) as
    select 2, 7 from dual union all
    select 1, 6 from dual union all
    select 4, 6 from dual union all
    select 3, 4 from dual union all
    select 7, 1 from dual union all
    select 5, 1 from dual union all
    select 8, 1 from dual union all
    select 6, 1 from dual;
    
    with data as (
      select id, val, row_number() over(order by val desc, id) rn
      from t
    )
    select id, val,
    case mod(ceil(rn/4)-1,2)
      when 0 then mod(rn-1,4)+1
      else 4 - mod(rn-1,4)
    end bin
    from data;
    
    ID VAL BIN
    2 7 1
    1 6 2
    4 6 3
    3 4 4
    5 1 4
    6 1 3
    7 1 2
    8 1 1
  • How to pragmatically set the name of the user event?

    In LabVIEW 2010, I register a user event during execution by using the function of events Reg, but I want to put the name of the user event, according to the runtime. LV said "If wire you a cluster at the entrance to the data type of the user events , LabVIEW uses the type name of the cluster in the event user name." But I need set the name of the event based on the data that is generated during execution.

    How can I pragmatically set the name of a user with data from cluster event at run time?

    I can do this by changing the text of the label of the bunch before I use Reg events function?

    How will I know what name the user never received?

    Thank you.

    I found a viable solution. I can create a user with a name known event. The event that is sent with the event data will include a Source DN string that identifies the sender of the event. The event handler can then handle each event based on the name of the Source. This is equivalent to have several unique named events, is only right they all share a common event data format, which is OK in my case.

    It works very well.

    THANKS for all the suggestions.

  • Divide the extracted almost lines also and assign a group

    Hi all

    I have the table:

    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0-
    SQL> select * from upd;
    
             A B
    ---------- ----------
             1 xxxx
             2 xxxx
             3 ddd
             4 d
             5 t
    Suppose I have only 5 groups and each group can have only 2 rows, example:

    assign the 2 rows to G1, G2 and G3 2 ranks, still something like
          A B            Group
    ---------- ---------- 
             1 xxxx       G1
             2 xxxx       G1
             3 ddd        G2
             4 d            G2
             5 t            G3
    Help, please

    Published by: user8650395 on November 8, 2009 01:13

    Hello

    Analytical NTILE function could do that:

    SQL> with upd as (  -- generating sample data:
      2  select 1 a, 'xxxx' b from dual union all
      3  select 2, 'xxxx' from dual union all
      4  select 3, 'ddd' from dual union all
      5  select 4, 'd' from dual union all
      6  select 5, 't' from dual
      7  )
      8  --
      9  -- actual query starts here:
     10  --
     11  select a
     12  ,      b
     13  ,      'G'||ntile(3) over (order by a) grp
     14  ,      ntile(3) over (order by a) ntile_result
     15  from   upd;
    
             A B    GRP                                       NTILE_RESULT
    ---------- ---- ----------------------------------------- ------------
             1 xxxx G1                                                   1
             2 xxxx G1                                                   1
             3 ddd  G2                                                   2
             4 d    G2                                                   2
             5 t    G3                                                   3
    

    See: http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions101.htm#SQLRF00680

  • Analytical function, NTILE

    Hi gurus

    I was reading documentation on the function analytical ntile, but I've understood... I really appreciate if someone explains in simple terms, for example...

    I also show the example I was reading, see below:

    Query

    SELECTemployee_name
           ,salary
           ,department
           ,NTILE(4) OVER (ORDERBYsalary) ASQuartile
    FROMemployee;
    Output

    Employee_Name salary Quartile Department

    EMP A 10000 sales 1

    EMP: 12000 sales 1

    EMP G 12000 sales 2

    EMP B 20000 IT 2

    EMP F 20000 sales 3

    EMP C 28000 IT 3

    Support D 30000 4 EMP

    EMP E 32000 4 sale

    If you see the above output, then you noticed that Emp G and B EMP times attribute 2 and this thing is really confusing. Thanks in advance

    Concerning

    Muzz

    The same case, explained in the documentation:

    The following example divides into 4 buckets the values in the salary column of the oe.employees table service 100. The salary column has 6 values in this Department, if the two extra values (the rest of 6 / 4) are assigned to buckets 1 and 2, which therefore have a greater value than buckets 3 or 4.

    SELECT name, salary, NTILE (4) OVER (ORDER BY salary DESC)

    As employees OF THE quartile

    WHERE department_id = 100;

    LAST_NAME SALARY QUARTILE

    ------------------------- ---------- ----------

    Greenberg 12000 1

    Faviet 9000 1

    Chen 8200 2

    Urman 7800 2

    Sciarra 7700 3

    Popp 6900 4

    So in your case the number of lines is 8 and you're dividing with 4 buckets. Yes 2 buckets for each line.

  • Reg, erred in function

    Hi all

    I wanted to write a function that indicates true if the table is empty, I tried below, but it is said table or view didn't exist how we code in this case:

    CREATE OR REPLACE FUNCTION ISTABLEEMPTY (TBLNM IN VARCHAR2)
    RETURN AS BOOLEAN
    TB_name varchar2 (10): = TBLNM;
    CNT NUMBER (10);
    BEGIN
    COUNT (*) SELECT IN CNT FROM tb_name;
    IF THE NTC IS NULL THEN
    RETURN TRUE;
    ON THE OTHER
    RETURN FALSE;
    END IF;
    END ISTABLEEMPTY;

    regs,
    Anthony.

    Hmm...

    satyaki>
    satyaki>select * from v$version;
    
    BANNER
    ----------------------------------------------------------------
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod
    PL/SQL Release 10.2.0.3.0 - Production
    CORE    10.2.0.3.0      Production
    TNS for 32-bit Windows: Version 10.2.0.3.0 - Production
    NLSRTL Version 10.2.0.3.0 - Production
    
    Elapsed: 00:00:00.20
    satyaki>
    satyaki>
    satyaki>CREATE OR REPLACE FUNCTION ISTABLEEMPTY(TBLNM IN VARCHAR2)
      2  RETURN BOOLEAN AS
      3    tb_name varchar2(10) := TBLNM;
      4    CNT NUMBER(10);
      5  BEGIN
      6    execute immediate 'SELECT COUNT(*) FROM '||tb_name INTO CNT;
      7     IF CNT = 0 THEN
      8      RETURN TRUE;
      9     ELSE
     10      RETURN FALSE;
     11     END IF;
     12  END ISTABLEEMPTY;
     13  /
    
    Function created.
    
    Elapsed: 00:00:04.04
    satyaki>
    satyaki>
    satyaki>declare
      2       a  boolean;
      3     begin
      4       a := ISTABLEEMPTY('&tb_nm');
      5       if a = true then
      6          dbms_output.put_line('Empty....');
      7       else
      8          dbms_output.put_line('Non Empty.....');
      9       end if;
     10     end;
     11  /
    Enter value for tb_nm: emp
    old   4:      a := ISTABLEEMPTY('&tb_nm');
    new   4:      a := ISTABLEEMPTY('emp');
    Non Empty.....
    
    PL/SQL procedure successfully completed.
    
    Elapsed: 00:00:00.45
    satyaki>
    satyaki>create table tt_ss_s
      2     as
      3       select * from emp where 1=2;
    
    Table created.
    
    Elapsed: 00:00:02.75
    satyaki>
    satyaki>
    satyaki>declare
      2    a  boolean;
      3  begin
      4    a := ISTABLEEMPTY('&tb_nm');
      5    if a = true then
      6       dbms_output.put_line('Empty....');
      7    else
      8       dbms_output.put_line('Non Empty.....');
      9    end if;
     10  end;
     11  /
    Enter value for tb_nm: tt_ss_s
    old   4:   a := ISTABLEEMPTY('&tb_nm');
    new   4:   a := ISTABLEEMPTY('tt_ss_s');
    Empty....
    
    PL/SQL procedure successfully completed.
    
    Elapsed: 00:00:00.12
    satyaki>
    

    Kind regards.

    LOULOU.

  • Reg: Error using Parallel_Enable in pipeline, function.

    Hi Experts,

    This might be a simple question, surely, I forgot something.

    CREATE or REPLACE PACKAGE parallel_pipelined_update AS

    TYPE rec_x IS RENDERING)
    rep_per NUMBER,
    unikid NUMBER
    );

    TYPE histdetl_tab IS TABLE OF THE rec_x

    INDEX BY PLS_INTEGER;

    -TYPE histdetl_tab IS TABLE OF THE histdetl;


    TYPE histdetl_refcur IS REF CURSOR RETURN histdetl;

    FUNCTION fn_parallel_update (input_cur_query IN histdetl_refcur)
    Histdetl_tab RETURN PIPELINED
    PARALLEL_ENABLE (PARTITION input_cur_query IN ALL);

    END parallel_pipelined_update;
    /

    During execution of this error are-

    Line of text Pos

    1 pack of Create, executed in 0.24 sec.

    14 12 PLS-00630: pipeline functions must have a collection of supported return type

    The total run of dry weather 1.16.

    What may be a possible reason? Please notify.

    -Nordine

    (on Oracle 11.2.0.3.0)

    To be more precise, who made the statement of histdetl_tab a table nested (as opposed to a PL/SQL table) would be supported.

    TYPE histdetl_tab IS TABLE OF rec_x INDEX OF PLS_INTEGER;

  • Reg: Using the BITAND function.

    Hi Experts,

    I have a doubt as to the use (by Frank) BITAND in this post - Re: need help for query oracle

    You know exactly how the BITAND here.

    Could you please help me understand this function?

    Thank you

    -Nordine

    ( on Oracle 11.2.0.3.0 )

    It's all to do with the Bits and bytes and binary logic...

    Value of 32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1
    ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----    -----
    0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16384
    0     0     1     0     0     0     0     0     0     0     0     0     0     0     0     0       8192
    0     0     0     0     1     0     0     0     0     0     0     0     0     0     0     0       2048
    0     0     0     0     0     0     1     0     0     0     0     0     0     0     0     0        512
    0     0     0     0     0     0     0     0     1     0     0     0     0     0     0     0        128
    0     0     0     0     0     0     0     0     0     0     0     0     0     1     0     0          4
    0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     0          2
    0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1          1

    BIT GOLD together is the same by adding up all the values in the set (oracle does not provide a function BEST unfortunately well ' + ' will do)... which gives

    0 1 1 0 1 0 1 0 1 0 0 0 0 1 1 1 27271

    If we want to know if there is a particular value in our value, we can use BITAND, so let's use BITAND (27271, 512):

    0 1 1 0 1 0 1 0 1 0 0 0 0 1 1 1 27271
    0     0     0     0     0     0     1     0     0     0     0     0     0     0     0     0        512
    Result:
    0     0     0     0     0     0     1     0     0     0     0     0     0     0     0     0        512

    the logic 1 and 1 on the ILO representative 512 in two values being 1, all other values ARE a logical AND operation of 1 and 0 resulting in 0

    Our result is 512, we know that the bit that represents 512 is set.

    Similarly, we can check for several bits of the number...

    BITAND (27271, 8192 + 512 + 1)

    0 1 1 0 1 0 1 0 1 0 0 0 0 1 1 1 27271
    0     0     1     0     0     0     1     0     0     0     0     0     0     0     0     1       8705
    Result:
    0     0     1     0     0     0     1     0     0     0     0     0     0     0     0     1       8705

    So we can say that all of the bits are set because our result is equal to the value we tested

    Let's try with: BITAND (27271, 8192 + 1024 + 128 + 1) to test all 4 of these bits

    0 1 1 0 1 0 1 0 1 0 0 0 0 1 1 1 27271
    0     0     1     0     0     1     0     0     1     0     0     0     0     0     0     1       8705
    Result:
    0     0     1     0     0     0     0     0     1     0     0     0     0     0     0     1       8321

    The result is not him that we have tested, is not a complete match, so we treat as a mismatch if we wanted all the 4 bits set.

    In general, people use the power of 2 amounts and BITAND to store 'switches' in a single numeric value, where you just need to know if something is on it or OFF.  Each switch is assigned a value that is a power of 2 which is unique compared to other switches.  Adding them together gives us a unique value to store and use BITAND against this value, we can test if the selector is set on ON or OFF.

  • Reg: Functions in ODI

    Hi all

    We all functions like deduplicate function in ODI?

    Kind regards
    Knockaert

    So this should work (verify that the alias of your data store is set to STUDENT_DETAILS):

    STUDENT_DETAILS.STUDENT_ID = (select max(b.STUDENT_ID) from STUDENT_DETAILS b where STUDENT_ID.STUDENT_NAME = b.STUDENT_NAME)
    

    If this isn't the case, please provide the generated code and the error message.

    This assumes that each student has a single unique STUDENT_NAME.

  • Reg - Index of the functional basis

    Hi all
    In our application, we have a clue of the type below
    CREATE INDEX sme_idx ON sme_tbl(sme_col,1,2)
    And when I view its details in ALL_INDEXES noted as NORMAL of BASIC FUNCTIONAL index, from my first glance I misread it as SUBSTR, but I'm not able to represent something of the syntax.

    What does '1.2' mean here? What is the use of having this kind of clue?

    Please help me.

    He held a function-based index, because the right-hand columns are constants.

    If it is not uncommon to have a constant leak like that to be sure that nulls in SME_COL are included in the index, I don't understand why you would have two constant end. I would have thought that it was an error, typo, or just a complete misunderstanding.

  • Reg. Which function to use.

    Hello
    I have a column for example emp_id of emp_id like 100 c 9, 10 s 010, 10008, 1e0011 like this. I want to only display the number as 10008,1009 with skip the column tank and tank is chnaging position all the time what oracle function will take care of it. and how to reach expexted result.

    appriiciate your answer.


    Thank you

    Published by: user13301356 on June 27, 2011 02:01

    Hello

    You can try this:

     select regexp_replace('1A25f3','[A-Za-z]') txt from dual;
    
    TXT
    ----
    1253
    

    Kind regards
    Sylvie

  • Reg: Ref cursor in function

    Hi friends,
    LSPQ HELP ME.
    I CREATED A FUNCTION AS EXAMPLE BELOW.

    TYPE REFCURTYPE IS REFCURSOR; -DECLARE REF CURSOR.

    CREATE FUNCTION FUNC1(EMPID,DESIG) RETURN REFCURTYPE
    AS
    OPEN FOR REFCURTYPE
    SELECT A GROUP OF GROUPMASTER;
    RETURN REFCURTYPE;
    END;

    WHEN I RUN THE QUERY AS
    SELECT FUNC1('1001','SM') FROM DUAL;

    IN THIS CASE, I WANT TO SEE THE REPORTS THAT IS COMES TO WORK IN REFCURTYPE... WHAT WOULD I DO?;

    Rajnish Chauhan says:
    BUT EXPENSIVE...

    I CALL THIS QUERY IN THE PROCEDURE... SELECT FUNCTION('101','SM') FROM DUAL;

    There is no need to SELECT. Just call function and fetch:

    SQL> CREATE OR REPLACE
      2    PROCEDURE P1
      3    IS
      4        v_cur SYS_REFCURSOR;
      5        v_ename VARCHAR2(20);
      6    BEGIN
      7        v_cur := f1;
      8        LOOP
      9          FETCH v_cur INTO v_ename;
     10          EXIT WHEN v_cur%NOTFOUND;
     11          DBMS_OUTPUT.PUT_LINE(v_ename);
     12        END LOOP;
     13  END;
     14  /
    
    Procedure created.
    
    SQL> set serveroutput on
    SQL> exec p1;
    SMITH
    ALLEN
    WARD
    JONES
    MARTIN
    BLAKE
    CLARK
    SCOTT
    KING
    TURNER
    ADAMS
    JAMES
    FORD
    MILLER
    
    PL/SQL procedure successfully completed.
    
    SQL> 
    

    SY.

Maybe you are looking for