Select rownum 1

Hi all

I have table that represent the cam of the job, and a worker's 111222333

other 111222888 are H start holiday (01/06/2015 to 07/06/2015 when workers return to work)

W's when it works.

Question is how calculate on 01/06/2015-07/06/2015 = 6 days holiday (start with H)?

with mydata as

(

Select to_date (June 9, 2015 ',' DD/MM/YYYY ') dt, - 684002410 s, 'W', versus, 111222333 code of dual union all

Select to_date (7 June 2015 ',' DD/MM/YYYY '), - 684050570, 'W', 111222333 of double union all

Select to_date (7 June 2015 ',' DD/MM/YYYY '), - 684050570, 'W', 111222888 of double union all

Select to_date (June 6, 2015 ',' DD/MM/YYYY '), - 684150570, 'W', 111222888 of double union all

Select to_date (5 June 2015 ',' DD/MM/YYYY '), - 684190570, 'W', 111222888 of double union all

Select to_date (June 4, 2015 ',' DD/MM/YYYY '), - 684220570, 'W', 111222888 of double union all

Select to_date (3 June 2015 ',' DD/MM/YYYY '), - 684270570, 'W', 111222888 of double union all

Select to_date (2 June 2015 ',' DD/MM/YYYY '), - 684310570, 'W', 111222888 of double union all

Select to_date (1 June 2015 ',' DD/MM/YYYY '), - 684338692, 'H', 111222333 of double union all

Select to_date (1 June 2015 ',' DD/MM/YYYY '), - 684348692, 'W', 111222333 of double union all

Select to_date (1 June 2015 ',' DD/MM/YYYY '), - 684350570, 'W', 111222888 of double union all

Select to_date (1 June 2015 ',' DD/MM/YYYY '), - 684350841, 'W', 111222333 of double union all

Select to_date (1 June 2015 ',' DD/MM/YYYY '), - 684378694, 'W', 111222888 of double union all

Select to_date (29 may 2015 ',' DD/MM/YYYY '), - 684611322, 'W', 111222333 of double union all

Select to_date (29 may 2015 ',' DD/MM/YYYY '), - 684614272, 'W', 111222888 of double union all

Select to_date (29 may 2015 ',' DD/MM/YYYY '), - 684615003, 'W', 111222333 of double union all

Select to_date (29 may 2015 ',' DD/MM/YYYY '), - 684637550, 'W', 111222888 of double union all

Select to_date (28 may 2015', ' MM/DD/YYYY ' "), - 684694292, 'W', 111222333 of the double

)

Select dt, dry

, to_date (6 February 2037 08:28:17 ',' DD/MM/YYYY hh24:mi:ss') + numtodsinterval(sec,'SECOND') val

, to_date (May 31, 2005 ',' DD/MM/YYYY ') + 0.2789 + numtodsinterval (s + 1000000000, 'SECOND') val2, versus, code

of mydata;

concerning

Gordan

Hello

You can use the function of leader to access the values of the following line as follows

with mydata as

(

Select to_date (June 9, 2015 ',' DD/MM/YYYY ') dt, - 684002410 s, 'W', versus, 111222333 code of dual union all

Select to_date (7 June 2015 ',' DD/MM/YYYY '), - 684050570, 'W', 111222333 of double union all

Select to_date (7 June 2015 ',' DD/MM/YYYY '), - 684050570, 'W', 111222888 of double union all

Select to_date (June 6, 2015 ',' DD/MM/YYYY '), - 684150570, 'W', 111222888 of double union all

Select to_date (5 June 2015 ',' DD/MM/YYYY '), - 684190570, 'W', 111222888 of double union all

Select to_date (June 4, 2015 ',' DD/MM/YYYY '), - 684220570, 'W', 111222888 of double union all

Select to_date (3 June 2015 ',' DD/MM/YYYY '), - 684270570, 'W', 111222888 of double union all

Select to_date (2 June 2015 ',' DD/MM/YYYY '), - 684310570, 'W', 111222888 of double union all

Select to_date (1 June 2015 ',' DD/MM/YYYY '), - 684338692, 'H', 111222333 of double union all

Select to_date (1 June 2015 ',' DD/MM/YYYY '), - 684348692, 'W', 111222333 of double union all

Select to_date (1 June 2015 ',' DD/MM/YYYY '), - 684350570, 'W', 111222888 of double union all

Select to_date (1 June 2015 ',' DD/MM/YYYY '), - 684350841, 'W', 111222333 of double union all

Select to_date (1 June 2015 ',' DD/MM/YYYY '), - 684378694, 'W', 111222888 of double union all

Select to_date (29 may 2015 ',' DD/MM/YYYY '), - 684611322, 'W', 111222333 of double union all

Select to_date (29 may 2015 ',' DD/MM/YYYY '), - 684614272, 'W', 111222888 of double union all

Select to_date (29 may 2015 ',' DD/MM/YYYY '), - 684615003, 'W', 111222333 of double union all

Select to_date (29 may 2015 ',' DD/MM/YYYY '), - 684637550, 'W', 111222888 of double union all

Select to_date (28 may 2015', ' MM/DD/YYYY ' "), - 684694292, 'W', 111222333 of the double

)

Select dt, dry

, to_date (6 February 2037 08:28:17 ',' DD/MM/YYYY hh24:mi:ss') + numtodsinterval(sec,'SECOND') val

, to_date (May 31, 2005 ',' DD/MM/YYYY ') + 0.2789 + numtodsinterval (s + 1000000000, 'SECOND') val2, versus, code

, DECODE (versus, 'H', LEAD(dt, 1) OVER (PARTITION BY code ORDER BY dt) - dt, null) holiday

from mydata by code, dt;

Concerning

Salim

Tags: Database

Similar Questions

  • Reg: Rownum

    Hi Master,

    I executed the query below... I had the result... then run another query (ofcourse even) but get the diff result set. Please explain to me... !!

    SQL > Select rownum from emp;

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    14 selected, lines

    SQL > select rownum separate from emp;

    1

    6

    11

    13

    2

    14

    4

    5

    8

    3

    7

    9

    10

    12

    14 selected lines.

    In the second query... What is going on.. why it does not appear in the order of the CSA? Please explain to me.

    Concerning

    AR

    As Scott and Nick, Oracle uses a unique hash to generate the values of distinct rownum:

    SQL > select rownum from scott.emp;

    Execution plan

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

    Hash value of plan: 3286312482

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

    | ID | Operation | Name | Lines | Cost (% CPU). Time |

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

    |  0 | SELECT STATEMENT |        |    14.    1 (0) | 00:00:01 |

    |  1.  COUNTY |        |       |          |           |

    |  2.  INDEX SCAN FULL | PK_EMP |    14.    1 (0) | 00:00:01 |

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

    SQL > select rownum separate

    2 from scott.emp;

    Execution plan

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

    Hash value of plan: 2421365613

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

    | ID | Operation | Name | Lines | Cost (% CPU). Time |

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

    |  0 | SELECT STATEMENT |        |    14.    2 (50) | 00:00:01 |

    |  1.  UNIQUE HASH |        |    14.    2 (50) | 00:00:01 |

    |  2.  COUNTY |        |       |           |          |

    |  3.    INDEX SCAN FULL | PK_EMP |    14.    1 (0) | 00:00:01 |

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

    Note the UNIQUE HASH in the second plan.  Essentially, it's what he does to get unique values (although the hashing used by the ora_hash algorithm seems to be different from the one used internally):

    SQL > select rn, ora_hash (rn)

    2 from (select rownum scott.emp rn)

    3 order by 2;

    RN ORA_HASH (RN)

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

    5 365452098

    7 738226831

    6 1021760792

    10 1237562873

    12 1646821573

    9 1706589901

    2 2064090006

    1 2342552567

    11 2671099528

    3 2706503459

    14 2791491783

    13 3006123492

    4 3217185531

    8 3510633070

    John

  • How to order rownum?

    Hello!

    I have a simple code that selects the fields in a table:

    SELECT
      ROWNUM,
      question,
      APEX_ITEM.RADIOGROUP(ROWNUM, YES_SCORE, 2,  apex_lang.message('YES'), p_item_id => ROWNUM||'y') yes,
      APEX_ITEM.RADIOGROUP(ROWNUM, NO_SCORE, 2,  apex_lang.message('NO'),  p_item_id => ROWNUM||'n') no
    FROM question q
    WHERE question.id = :TYPE 
    ORDER BY ORDER_ID,ROWNUM;
    

    Now, I had insert an ORDER_ID field in the table to make the admin output lines in certain order and Add ORDER BY line code.

    Who messed up the column ROWNUM.

    Add column rownum differently to get the line numbers appear in the proper order (1, 2, 3,...)?

    IM using APEX 4.2.1.

    Hello

    You may

    SELECT rownum

    x.question

    APEX_ITEM. RADIOGROUP (rownum

    x.yes_score

    2

    apex_lang.message ('YES')

    p_item_id-online rownum | (' y') Yes

    APEX_ITEM. RADIOGROUP (rownum

    x.no_score

    2

    apex_lang.message ('NO')

    p_item_id-online rownum | ("no) no

    FROM (SELECT q.question

    q.yes_score

    q.no_score

    QUESTION q

    WHERE q.question.id =: TYPE

    ORDER BY q.order_id

    ) x

    ;

    (I assumed that yes_score and no_score are the 'question' column; suggestion: use the alias for the table names and precede all columns with the corresponding alias)

    Best regards

    Bruno Vroman

  • You have no lines for the SQL query to select

    Dear Sir.

    I don't want to choose these lines in the second query, please guide me how to do.

    Request-> 1

    (SELECT ID_NO, ATTND_DATE
        FROM ATTND_REGISTER 
        WHERE ATTND_DATE BETWEEN :FD AND :TD
        ORDER BY ID_NO, ATTND_DATE
    )AT
    

    A query result.

    ID_NOATTND_DATE
    E1FEBRUARY 1, 14
    E1FEBRUARY 2, 14
    E13 FEBRUARY 14
    E14 FEBRUARY 14
    E1FEBRUARY 5, 14
    E1FEBRUARY 6, 14
    E1FEBRUARY 7, 14
    E1FEBRUARY 8, 14
    E1FEBRUARY 9, 14
    E110 FEBRUARY 14
    E1FEBRUARY 11, 14
    E1FEBRUARY 12, 14
    E1FEBRUARY 13, 14
    E114 FEBRUARY 14
    E115 FEBRUARY 14
    E116 FEBRUARY 14
    E117 FEBRUARY 14
    E118 FEBRUARY 14
    E119 FEBRUARY 14
    E120 FEBRUARY 14
    E4FEBRUARY 1, 14
    E4FEBRUARY 2, 14
    E43 FEBRUARY 14
    E44 FEBRUARY 14
    E4FEBRUARY 5, 14

    Request-> 2

    : VALUE OF THE VARIABLE FD = 1 FEBRUARY 14

    : VALUE OF THE VARIABLE TD = FEBRUARY 28, 14 '

    SELECT A.ID_NO,B.DATES,A.EMP_NAME, A.DESG_NAME
    FROM
      (
        SELECT  ROWNUM - 1 + TO_DATE(:FD, 'DD/MM/RR') DATES
        FROM ALL_OBJECTS
        WHERE ROWNUM < TO_DATE(:TD, 'DD/MM/RR') - TO_DATE(:FD, 'DD/MM/RR') + 2
      )B,
    
    
      (
        SELECT ID_NO, INITCAP(FL_NAME) AS EMP_NAME, INITCAP(UBN.DESG_NAME(DESG_ID)) AS DESG_NAME
        FROM PERSONAL
        WHERE (STATUS = 'A')
        AND (COMPANY_CODE = :COMPANY_CODE)---COMPANY CODE=6
        AND (ID_NO = :ID_NO OR :ID_NO IS NULL)
      )A
    ORDER BY A.ID_NO,B.DATES;
    


    Solved

    I changed in the second query with using the first query.

    in Where Clause

    WHERE B.DATES | A.ID_NO NOT IN (SELECT ATTND_DATE |) NBI ID_NO. ATTND_REGISTER

    WHERE ATTND_DATE BETWEEN: FD AND: TD)

    Thank you Allah (SWT).

  • Abnormal (?) rownum

    Hi all

    Could you please help me understand how calculated on the last column of the first and the last line?

    SQL> select rownum,rownum-1,rownum-2,rownum||rownum-1||rownum-2 from dual connect by level < 11;
        ROWNUM   ROWNUM-1   ROWNUM-2 ROWNUM||ROWNUM-1||ROWNUM-2
    ---------- ---------- ---------- --------------------------
             1          0         -1                         99
             2          1          0                        210
             3          2          1                        321
             4          3          2                        432
             5          4          3                        543
             6          5          4                        654
             7          6          5                        765
             8          7          6                        876
             9          8          7                        987
            10          9          8                     100908
    10 rows selected.
    

    Sorry, didn't read your post carefully

    darul75 wrote:

    That didn't work but below any idea of what is going on there?

    Yes, I know exactly what is happening... the order of operations is confused, you...

    what you trying to accomplish?

    Oracle operations of channels first, therefore:

    a+b||c+d||e+f
    

    transformed into:

    a + (b||(c+d)||e) + f
    

    [edit]

    If you try only to tie them. Use several media:

    (rownum) | (rownum-1) | (rownum-2)

  • Question of rowNum

    Dear friends

    Given an array of simple employees with the names of employees and wages.

    for this query:

    Select rownum, salary
    from (select name, salary
    employees
    salary desc order)

    .. I am able to see the number of lines like 1,2,3,4,5...
    and the names of the employees and wages in descending order,

    However if I try to use:
    Select rownum, salary
    from (select name, salary
    employees
    salary desc order)
    where rownum = 2

    Why didn't he give me the second highest salary?

    In the case of:
    Select rownum, salary
    from (select name, salary
    employees
    salary desc order)
    where rownum = 1
    .. I am able to see the highest salary.


    Thanks for your help...

    Best

    Rownum is assigned to the lines of recoveries, so it always starts with 1. So query with ROWNUM = N where N > 1 will return all the lines. Use:

    select  first_name,
            salary
      from  (
             select  first_name,
                     salary,
                     rownum rn
               from  (
                      select  first_name,
                              salary
                        from  employees
                        order by salary desc
                     )
            )
      where rn = 2
    /
    

    Or better:

    select  first_name,
            salary
      from  (
             select  first_name,
                     salary,
                     row_number() over(order by salary desc) rn
               from  employees
            )
      where rn = 2
    /
    

    And don't forget, your query (identical to the ones I posted above) does not return second highest salary. Then, they return more employee paid.

    SY.

  • How to order by working with rownum

    Oracle worm: 11g

    Hi all
    I have 2 cases here, with external control by one and another without external by command.
    I see no difference in the results for the moment.
    Case 1:
    /* Formatted on 4/30/2013 2:12:28 PM (QP5 v5.240.12305.39446) */
    SELECT *
      FROM (  SELECT ROWNUM rn, id
                FROM (SELECT 9 AS id FROM DUAL
                      UNION ALL
                      SELECT 1 FROM DUAL
                      UNION ALL
                      SELECT 14 FROM DUAL
                      UNION ALL
                      SELECT 7 FROM DUAL
                      UNION ALL
                      SELECT 5 FROM DUAL
                      UNION ALL
                      SELECT 6 FROM DUAL
                      UNION ALL
                      SELECT 13 FROM DUAL
                      UNION ALL
                      SELECT 8 FROM DUAL
                      UNION ALL
                      SELECT 2 FROM DUAL
                      UNION ALL
                      SELECT 4 FROM DUAL
                      UNION ALL
                      SELECT 19 FROM DUAL
                      UNION ALL
                      SELECT 3 FROM DUAL)
            ORDER BY id)
     WHERE rn BETWEEN 1 AND 6;
     
    Case 2:
    I added the command by external clause in this case
    But I want to make sure if I really need to have the order of outer query by here.
    /* Formatted on 4/30/2013 2:12:28 PM (QP5 v5.240.12305.39446) */
    SELECT *
      FROM (  SELECT ROWNUM rn, id
                FROM (SELECT 9 AS id FROM DUAL
                      UNION ALL
                      SELECT 1 FROM DUAL
                      UNION ALL
                      SELECT 14 FROM DUAL
                      UNION ALL
                      SELECT 7 FROM DUAL
                      UNION ALL
                      SELECT 5 FROM DUAL
                      UNION ALL
                      SELECT 6 FROM DUAL
                      UNION ALL
                      SELECT 13 FROM DUAL
                      UNION ALL
                      SELECT 8 FROM DUAL
                      UNION ALL
                      SELECT 2 FROM DUAL
                      UNION ALL
                      SELECT 4 FROM DUAL
                      UNION ALL
                      SELECT 19 FROM DUAL
                      UNION ALL
                      SELECT 3 FROM DUAL)
            ORDER BY id)
     WHERE rn BETWEEN 1 AND 6;
     ORDER BY id;
    Thank you
    Rod.

    Hey, Rod,

    SamFisher wrote:
    ... I see no difference in the results for the moment.

    You can not, but you are not sure that you will never see a difference. There may be a difference if you change versions, or if you add more data, or for no apparent reason.

    If there is an ORDER BY clause in the main query (as in query 2) then the output is guaranteed to be in that order.
    Is there no ORDER BY clause in the main query (as in the query 1), then there is no guarantee that the lines will be in a particular order. If Oracle finds it convenient to display rows in order by one of the columns (for example, rn) then she will do. You can't count on it always does the same thing, unless there is an ORDER BY clause.

    You should check the ROW_NUMBER analytic function. It is much more useful and easier to understand, as ROWNUM. (In this example, it maybe not simple, however.)

  • Is it possible to use the ROWNUM in a subquery?

    I wonder if it is legal to use rownum (for example, a generator of line) in a subquery. I have not found, it is illegal. But it doesn't seem to work without danger

    Case of work
    SQL> with v as (select rownum r from (select 1 from dual union all select 2 from dual)) select t1.r t1, t2.r t2 from v t1, v t2
    
            T1         T2
    ---------- ----------
             1          1
             1          2
             2          1
             2          2
    Case does not
    SQL> create table t(x) as select 1 from dual union all select 2 from dual
    Table created.
    SQL> with v as (select rownum r from t) select t1.r t1, t2.r t2 from v t1, v t2
    
            T1         T2
    ---------- ----------
             1          1
             1          2
             3          1
             3          2
    Published by: Laurent Schneider on March 26, 2013 13:12
    version 11.2.0.3 on aix

    I would say that there certainly is a glitch in the way that Oracle treats...

    SQL>  with v as (select rownum r from t) select t1.r t1, t2.r t2 from v t1, v t2
      2  /
    
            T1         T2
    ---------- ----------
             1          1
             2          2
             3          1
             3          2
    
    SQL>  with v as (select /*+ MATERIALIZE */ rownum r from t) select t1.r t1, t2.r t2 from v t1, v t2
      2  /
    
            T1         T2
    ---------- ----------
             1          1
             1          2
             2          1
             2          2
    
    SQL> ed
    Wrote file afiedt.buf
    
      1  select t1.r t1, t2.r t2
      2* from (select rownum r from t) t1, (select rownum r from t) t2
    SQL> /
    
            T1         T2
    ---------- ----------
             1          1
             1          2
             2          1
             2          2
    

    Something to do with the subquery factoring.

    The explain command plans look identical, so perhaps a trace would be something appear.

  • Save the implementation of counter as 1,2,3,4,5,1,2,3,4,5,1,2... use of rownum

    Hello
    Need to set up a check-in counter. the meter should be increased from 1 to 5, and then start return of 1. Thus, the record counter must be 1,2,3,4,5,1,2,3,4,5,1,2...
    Currently I use "select rownum meter, Id SurveyId in PB_SURVEY_USER_TEST;"
    How I can use rownum to get a record count as 1,2,3,4,5,1,2,3,4,5,1,2...
    Need help here...

    MOD (rownum-1, 5) + 1?

    Edited by: bencol February 1, 2013 12:04

  • Select SQL statement - See all the value of the range of month of entry

    Hi all

    I have a vision that is a union of other views
    But the description of the view is as below
    desc dashboard_monthly_view
    Name                           Null Type         
    ------------------------------ ---- ------------ 
    MONTHS                              VARCHAR2(17) 
    NUM_DEPENDENT_IN_ASSESSMENT         NUMBER       
    NUM_REFERRED_AODTC                  NUMBER       
    NUM_AT_DETERMINATION_HEARING        NUMBER       
    NUM_ACCEPTED                        NUMBER       
    NUM_NOT_ACCEPTED                    NUMBER       
    NUM_EXITED_SUCCESS                  NUMBER       
    AVERAGE_DAY_TO_EXIST                NUMBER       
    NUM_EXITED_UNSUCCESS                NUMBER       
    AVERAGE_DAY_TO_EXIST_UNSUCCESS      NUMBER       
    COURT_NAME                          VARCHAR2(9)  
    -current data in the view
    MONTHS            NUM_DEPENDENT_IN_ASSESSMENT NUM_REFERRED_AODTC     NUM_AT_DETERMINATION_HEARING NUM_ACCEPTED           NUM_NOT_ACCEPTED       NUM_EXITED_SUCCESS     AVERAGE_DAY_TO_EXIST   NUM_EXITED_UNSUCCESS   AVERAGE_DAY_TO_EXIST_UNSUCCESS COURT_NAME 
    ----------------- --------------------------- ---------------------- ---------------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ------------------------------ ---------- 
    AUG 2012          1                           0                      0                            0                      0                      0                      0                      0                      0                              AAA   
    OCT 2012          8                           1                      3                            1                      1                      1                      44                     1                      4                              AAA   
    SEP 2012          2                           0                      2                            2                      0                      0                      0                      0                      0                              AAA   
    *UNDEFINED*       0                           11                     7                            1                      1                      0                      0                      1                      0                              AAA   
    NOV 2012          0                           0                      0                            0                      0                      0                      0                      1                      54                             BBB  
    OCT 2012          4                           1                      2                            1                      1                      1                      9                      0                      0                              BBB  
    SEP 2012          1                           0                      0                            0                      0                      1                      14                     0                      0                              BBB  
    *UNDEFINED*       0                           5                      4                            1                      0                      0                      0                      1                      0                              BBB  
    AUG 2012          1                           0                      0                            0                      0                      0                      0                      0                      0                              COMBINED   
    NOV 2012          0                           0                      0                            0                      0                      0                      0                      1                      54                             COMBINED   
    OCT 2012          12                          2                      5                            2                      2                      2                      26.5                   1                      4                              COMBINED   
    SEP 2012          3                           0                      2                            2                      0                      1                      14                     0                      0                              COMBINED   
    *UNDEFINED*       0                           16                     11                           2                      1                      0                      0                      2                      0                              COMBINED   
    
     13 rows selected 
     
    -My select query is
     DEFINE startmonth = "Aug 2012";
    DEFINE endmonth   = "Nov 2012";
    with all_months as
    ( select to_char(which_month, 'MON YYYY') month from
      (select
            add_months(to_date('&startmonth','MON YYYY'), rownum-1) which_month
        from
            all_objects
        where
            rownum <= months_between(to_date(NVL('&endmonth', '&startmonth'),'MON YYYY'), add_months(to_date('&startmonth','MON YYYY'), -1))
        order by
            which_month )
    )
    
    select nvl(months, '**ALL**')    AS "MONTHS", TO_DATE(MONTHS, 'MON YYYY') AS MONTH_SORT
        , sum(num_dependent_in_assessment)    AS num_dependent_in_assessment
        , sum(num_referred_aodtc)    AS num_referred_aodtc
        , sum(num_at_determination_hearing) as num_at_determination_hearing
        , sum(num_accepted) AS num_accepted
        , sum (num_not_accepted) AS num_not_accepted
        , sum(num_exited_success) as num_exited_success
        , sum(average_day_to_exist) as average_day_to_exist
        , sum(num_exited_unsuccess) as num_exited_unsuccess
        , sum (average_day_to_exist_unsuccess) as average_day_to_exist_unsuccess
    from 
      DASHBOARD_MONTHLY_VIEW    right outer join all_months
      on DASHBOARD_MONTHLY_VIEW.months = all_months.month
    --where months in (select month from all_months)
      and upper(court_name) like 'AAA'
    group by (months)
    order by month_sort
     
    - And the result is
    MONTHS            MONTH_SORT                NUM_DEPENDENT_IN_ASSESSMENT NUM_REFERRED_AODTC     NUM_AT_DETERMINATION_HEARING NUM_ACCEPTED           NUM_NOT_ACCEPTED       NUM_EXITED_SUCCESS     AVERAGE_DAY_TO_EXIST   NUM_EXITED_UNSUCCESS   AVERAGE_DAY_TO_EXIST_UNSUCCESS 
    ----------------- ------------------------- --------------------------- ---------------------- ---------------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ------------------------------ 
    AUG 2012          01/08/12                  1                           0                      0                            0                      0                      0                      0                      0                      0                              
    SEP 2012          01/09/12                  2                           0                      2                            2                      0                      0                      0                      0                      0                              
    OCT 2012          01/10/12                  8                           1                      3                            1                      1                      1                      44                     1                      4                              
    **ALL**                                                                                                                                                                                                                                                                       
    
     
    -The requirement of results I have to produce is
    MONTHS            MONTH_SORT                NUM_DEPENDENT_IN_ASSESSMENT NUM_REFERRED_AODTC     NUM_AT_DETERMINATION_HEARING NUM_ACCEPTED           NUM_NOT_ACCEPTED       NUM_EXITED_SUCCESS     AVERAGE_DAY_TO_EXIST   NUM_EXITED_UNSUCCESS   AVERAGE_DAY_TO_EXIST_UNSUCCESS 
    ----------------- ------------------------- --------------------------- ---------------------- ---------------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ------------------------------ 
    AUG 2012          01/08/12                  1                           0                      0                            0                      0                      0                      0                      0                      0                              
    SEP 2012          01/09/12                  2                           0                      2                            2                      0                      0                      0                      0                      0                              
    OCT 2012          01/10/12                  8                           1                      3                            1                      1                      1                      44                     1                      4                              
    NOV 2012          01/11/12                  0                           0                      0                            0                      0                      0                      0                      0                      0                          
    **ALL**
     
    On the tota (* EVERYTHING *) l, I tried to use the rollup but he total average too, which is not correct. I think the reason because he cannot read the form that was used to calculate the column.
    How can I fix this, should I create, select another below one, with the sum of each column and the average for the other columns.
    Also, the business analyst want to show all the months between the start and end of the month.
    I used the right outer join, but apparently does not produce the right result.
    If anyone of you have any ideas, please advise.
    We use Oracle 11 g, it is a select statement for an Oracle APEX report.
    The APEX version is 4.0.2. I'm a junior developer of the APEX and I still have to learn a lot about SQL Oracle analytic function.

    Thank you very much in advance.

    Ann

    Hi, Ann.

    Ann586341 wrote:
    ... I created a table to contain a simplified version of this view

    Thank you. It is much easier to work with.

    ... My query is

    DEFINE startmonth = "Aug 2012";
    DEFINE endmonth   = "Nov 2012";
    with all_months as
    ( select to_char(which_month, 'MON YYYY') month from
    (select
    add_months(to_date('&startmonth','MON YYYY'), rownum-1) which_month
    from
    all_objects
    where
    rownum <= months_between(to_date(NVL('&endmonth', '&startmonth'),'MON YYYY'), add_months(to_date('&startmonth','MON YYYY'), -1))
    order by
    which_month )
    )
    , tbl_dashboard_active as
    ( select *
    from tbl_dashboard_monthly
    where months != '**UNDEFINED**' )
    
    select tbl.months    AS "MONTHS" --, TO_DATE(tbl.MONTHS, 'MON YYYY') AS MONTH_SORT
    , tbl.num_hearing
    , tbl.num_exited_success
    , tbl.avg_day_success_exist
    , tbl.num_exited_unsuccess
    , tbl.avg_day_unsuccess_exist
    
    from
    tbl_dashboard_active  tbl right outer join all_months am
    on tbl.months = am.month
    and upper(tbl.court_name) like 'BBB'
    
    UNION ALL
    
    select 'ALL'    AS "TOTAL"
    , SUM(tbl.num_hearing)
    , SUM(tbl.num_exited_success)
    , round(AVG(tbl.avg_day_success_exist),2)
    , SUM(tbl.num_exited_unsuccess)
    , round(AVG(tbl.avg_day_unsuccess_exist),2)
    --order by to_date(am.month,'MON YYYY')
    
    from
    tbl_dashboard_monthly  tbl right outer join all_months am
    on tbl.months = am.month
    and upper(tbl.court_name) like 'BBB'
    

    - And the result I got

    MONTHS            NUM_HEARING NUM_EXITED_SUCCESS AVG_DAY_SUCCESS_EXIST NUM_EXITED_UNSUCCESS AVG_DAY_UNSUCCESS_EXIST
    ----------------- ----------- ------------------ --------------------- -------------------- -----------------------
    AUG 2012                    1                  0                     0                    0                       0
    OCT 2012                    1                  0                     0                    2                      35
    SEP 2012                    1                  0                     0                    0                       0 
    
    ALL                         3                  0                     0                    2                   11.67 
    

    I don't know why even I already filter all lines that the month is undefined, I still have a blank line in the result set.

    This is the line for November. You do an outer join, in order to ensure that each value of am.month is displayed, even if it does not match what anyone in tbl. When it does not match anything, then all the columns tbl is supposed to provide will be NULL. You decide to view tbl. months, which is one of the following columns will be NULL. You should display mod. monmth instead.

    But if I run for handset Court, I don't see this problem. The reason is that the Court combined have given for all four months?

    When you say "on behalf of the combined Court", do you mean the unconditional "upper (tbl.court_name) as"BBB "?
    If Yes, that would explain it.

    Also is it possible to list all the months between the start and end month assuring the user even if the statistics are 0.

    Once again, in the case of lines that are present, even if they do not have the status of outer join, all of these columns will be NULL. Use NVL to map these nulls to 0.

    and how to sort the month

    GROUP OF two expressions, which depend on each other: one for sorting and the other for display.
    For the sort expression, you can use months as a DATE. (It seems that you have tried this, but commented on the ORDER BY clause in your query is before the FROM clause.) The ORDER BY clause is always at the end of the query, after the FROM clause.)
    Another expression of sorting is the number you used to generate the first month. That's what I used below.

    Here's a way to get the results you requested:

    WITH   got_months    AS
    (
         SELECT     TO_DATE ('&startmonth', 'Mon YYYY')     AS startmonth_dt
         ,     TO_DATE ( NVL ( '&endmonth'
                         , '&startmonth'
                         )
                   , 'Mon YYYY'
                   )                    AS endmonth_dt
         FROM    dual
    )
    ,     all_months     AS
    (
         SELECT  rownum               AS month_num
         ,     TO_CHAR ( ADD_MONTHS ( m.startmonth_dt
                                , ROWNUM - 1
                             )
                   , 'MON YYYY'
                   )              AS months
         FROM        got_months  m
         CROSS JOIN  all_objects
         WHERE     ROWNUM <= 1 + MONTHS_BETWEEN ( m.endmonth_dt
                                              , m.startmonth_dt
                                  )
    )
    SELECT    NVL ( am.months
               , 'All'
               )                              AS months
    ,        SUM (NVL (tbl.num_hearing,             0))     AS num_hearing
    ,        SUM (NVL (tbl.num_exited_success,      0))     AS num_exited_success
    ,       AVG (NVL (tbl.avg_day_success_exist,   0))     AS avg_day_success_exist
    ,       SUM (NVL (tbl.num_exited_unsuccess,    0))     AS num_exited_unsuccess
    ,       AVG (NVL (tbl.avg_day_unsuccess_exist, 0))     AS avg_day_unsuccess_exist
    FROM              all_months          am
    LEFT OUTER JOIN      tbl_dashboard_monthly  tbl  ON  am.months           = tbl.months
                                           AND  UPPER (tbl.court_name) = 'BBB'
    GROUP BY  GROUPING SETS ( (am.month_num, am.months)
                              , ()
                   )
    ORDER BY  am.month_num
    ;
    

    Again, I used two GROUP BY expressions: one for sorting, the other for display. These depend on each other, that is, given one, you could derive from each other, and it is not sensible to dependent ROLLUP GROUP BY expressions like that, so I used GROUPING SETS ROLLUP instead, so it would be only 1 rank of great aggregate (in other words, 'all').

    Output:

    `                               AVG_                 AVG_
                         NUM_       DAY_      NUM_       DAY_
                 NUM_ EXITED_    SUCCESS   EXITED_  UNSUCCESS
    MONTHS    HEARING SUCCESS     _EXIST UNSUCCESS     _EXIST
    --------- ------- ------- ---------- --------- ----------
    AUG 2012        1       0          0         0          0
    SEP 2012        1       0          0         0          0
    OCT 2012        1       0          0         2         35
    NOV 2012        0       0          0         0          0
    All             3       0          0         2       8.75
    
  • RowNum problem

    Dear all,

    Under query running perfectly

    SELECT ROWNUM AS RANK, name, SALARY, HIRE_DATE (HIRE_DATE SELECT first name, SALARY FROM EMPLOYEE ORDER BY SALARY desc, DESC HIRE_DATE)
    WHERE
    ROWNUM < = 3

    But when I try to find the third line of salary with the following query, then it returns null. Can someone tell me why it returns null and give me the same query.

    SELECT ROWNUM AS RANK, name, SALARY, HIRE_DATE (HIRE_DATE SELECT first name, SALARY FROM EMPLOYEE ORDER BY SALARY desc, DESC HIRE_DATE)
    WHERE
    ROWNUM = 3

    Try this

    select  * from (select first_name,salary,hiredate,row_number() over(order by salary desc) rn from employee)
    Where rn=3
    

    Not tested.

  • How to select the lines/odd table...

    How to select lines / odd in a table?
    Help, please.

    Published by: vaibhav on May 7, 2012 05:30

    This can be done by using the function mod like this:

    For even rows
    Select * from (select rownum rn, nom_table.* from tableName) where mod (rn, 2) = 0;

    For the odd lines
    Select * from (select rownum rn, nom_table.* from tableName) where mod (rn, 2) = 1;

  • SELECT - MODELING maybe clause can help?

    Hello world

    It's me again with another hierarchical statement. This time, my problem is quite complex. We have a scenario to explain the problem:
    DROP TABLE organisations;
    CREATE TABLE organisations
    (
      org_id NUMBER(10), 
      organ_name VARCHAR2(255), 
      parent_id NUMBER(10)
    );
    
    
    INSERT INTO organisations VALUES(1, 'TOP', NULL);
    INSERT INTO organisations VALUES(3, 'TOP2', NULL);
    
    INSERT INTO organisations VALUES(11, 'HR', 1);
    
    INSERT INTO organisations VALUES(12, 'Recruitment', 11);
    INSERT INTO organisations VALUES(19, 'Payroll', 11);
    
    INSERT INTO organisations VALUES(23, 'Evaluations', 12);
    
    INSERT INTO organisations VALUES(35, 'IT', 1);
    INSERT INTO organisations VALUES(39, 'Software', 35);
    
    INSERT INTO organisations VALUES(222, 'Windows', 39);
    INSERT INTO organisations VALUES(228, 'Linux', 39);
    INSERT INTO organisations VALUES(223, 'Mac OS', 39);
    
    INSERT INTO organisations VALUES(118, 'Applications', 39);
    
    INSERT INTO organisations VALUES(119, 'Photoshop', 118);
    INSERT INTO organisations VALUES(119, 'Microsoft Office 2010', 118);
    
    
    INSERT INTO organisations VALUES(29, 'Hardware', 35);
    INSERT INTO organisations VALUES(99, 'Helpdesk', 35);
    COMMIT;
    Examples of previous data is a single hierarchical table with a tree of the organization. A high level organization has no parent id and an organization is linked to its parent by parent_id. So far, nothing weird and very frequent structure.


    The SELECT statement returns the complete structure of the organizational tree.
    SELECT org_id, parent_id, LPAD(' ', LEVEL*2) || organ_name as indented_name
      FROM organisations 
     START WITH parent_id IS NULL
    CONNECT BY PRIOR org_id = parent_id;
    Here is the result:
    1            TOP
    11     1         HR
    12     11           Recruitment
    23     12             Evaluations
    19     11           Payroll
    35     1         IT
    29     35           Hardware
    39     35           Software
    118     39             Applications
    119     118               Photoshop
    119     118               Microsoft Office 2010
    222     39             Windows
    223     39             Mac OS
    228     39             Linux
    99     35           Helpdesk
    3            TOP2
    I would like to have a result like this:
    1            TOP
    11     1         HR
    11     1         HR.GROUP
    12     11           Recruitment
    12     11           Recruitment.GROUP
    23     12             Evaluations    -- no descendants, so no .GROUP node.
    19     11           Payroll
    35     1         IT
    35     1         IT.GROUP
    29     35           Hardware
    39     35           Software
    39     35           Software.GROUP
    118     39             Applications
    118     39             Applications.GROUP
    119     118               Photoshop
    119     118               Microsoft Office 2010
    222     39             Windows
    223     39             Mac OS
    228     39             Linux
    99     35           Helpdesk
    3            TOP2
    Rules to build this result are simple (yet complex to be implemented using SQL). Whenever a node descendant nodes, I need to have duplicate and the other must have exactly the same name with the suffix. GROUP. If a node is a terminal leaflet, it shouldn't have one. GROUP check-in. A top-level (TOP & TOP2) node does not take into account for this.

    No idea how we can implement this using SQL? I use 10g (so I can use the TYPE clause if it helps)


    Thank you guys

    Or use GROUP BY with GROUPING SETS:

    WITH t AS (
               SELECT ROWNUM rn,
                       level lvl,
                       org_id,
                       parent_id,
                       LPAD(' ', LEVEL*2) || organ_name as indented_name,
                       CONNECT_BY_ISLEAF leaf
                 FROM  organisations
                 START WITH parent_id IS NULL
                 CONNECT BY PRIOR org_id = parent_id
              )
    SELECT  max(org_id) org_id,
            max(parent_id) parent_id,
            max(indented_name) ||
              case grouping(lvl)
                when 1 then '.GROUP'
              end indented_name
      FROM  t
      GROUP BY GROUPING SETS((rn),(rn,lvl))
      HAVING grouping(lvl) = 0
          OR (
                  max(lvl) != 1
              and
                  max(leaf) = 0
             )
      ORDER BY rn,
               grouping(lvl)
    /
    
        ORG_ID  PARENT_ID INDENTED_NAME
    ---------- ---------- -------------------------------
             1              TOP
            11          1     HR
            11          1     HR.GROUP
            12         11       Recruitment
            12         11       Recruitment.GROUP
            23         12         Evaluations
            19         11       Payroll
            35          1     IT
            35          1     IT.GROUP
            29         35       Hardware
            39         35       Software
    
        ORG_ID  PARENT_ID INDENTED_NAME
    ---------- ---------- -------------------------------
            39         35       Software.GROUP
           118         39         Applications
           118         39         Applications.GROUP
           119        118           Photoshop
           119        118           Microsoft Office 2010
           222         39         Windows
           223         39         Mac OS
           228         39         Linux
            99         35       Helpdesk
             3              TOP2
    
    21 rows selected.
    
    SQL> 
    

    SY.

  • Insert select with the id auto incream

    CREATE TABLE LOG
    (NUMBER OF 'ID',
    VARCHAR2 (30 BYTE) "ROLEUSER."
    VARCHAR2 (30 BYTE) "ROLENAME",.
    "studentId" VARCHAR2 (11 BYTE)
    )
    then I insert in the newspaper (ROLEUSER, ROLENAME, studentID) (select ROLEUSER, ROLENAME, studentID of table a, b).
    where a.userid = buserid)
    I want to get every row I insert the id auto insert with number of automatic increase
    then I have insert into Log(ROLEUSER, ROLENAME, studentID)( select ROLEUSER, ROLENAME, studentID from table a, b
    

    Use

    insert into Log(ID, ROLEUSER, ROLENAME, studentID)( select ROWNUM, ROLEUSER, ROLENAME, studentID from table a, b
    where a.userid=buserid)
    
  • Number of rows returned by Rownum

    Hello

    When I run this code I get 8 rows where I see that the rownum is repeated twice. Why the rownum is repeats and if yes why alone2 times?
    set serveroutput on ;
     begin
        for item in
        (
          select rownum as r, first_name||' '||last_name as full_name,salary*10 as dream_salary from employees
          where rownum <5
          )
        loop
          dbms_output.put_line(item.full_name||'dreams having'||item.dream_salary||' '||item.r);
        end loop;
      end;
      /
    This is the result I get:
    Steven King dreams having 264600 1
    Neena Kochhar dreams having 170000 2
    Lex De Haan dreams having 170000 3
    Alexander Hunold dreams having 90000 4
    Bruce Ernst dreams having 60000 5
    Steven King dreams having 264600 1
    Neena Kochhar dreams having 170000 2
    Lex De Haan dreams having 170000 3
    Alexander Hunold dreams having 90000 4
    I have 107 lines.
    Employees of the table contains the following columns
     desc employees
    Name           Null     Type         
    -------------- -------- ------------ 
    EMPLOYEE_ID    NOT NULL NUMBER(6)    
    FIRST_NAME              VARCHAR2(20) 
    LAST_NAME      NOT NULL VARCHAR2(25) 
    EMAIL          NOT NULL VARCHAR2(25) 
    PHONE_NUMBER            VARCHAR2(20) 
    HIRE_DATE      NOT NULL DATE         
    JOB_ID         NOT NULL VARCHAR2(10) 
    SALARY                  NUMBER(8,2)  
    COMMISSION_PCT          NUMBER(2,2)  
    MANAGER_ID              NUMBER(6)    
    DEPARTMENT_ID           NUMBER(4)    
    LEFT_DATE               DATE         
    When I use-
           select rownum as r, first_name||' '||last_name as full_name,salary*10 as dream_salary from employees
          where rownum <=5
    I get 5 rows with the correct rownums.

    Thank you in advance

    Published by: to_learn on December 2, 2011 07:47

    My guess is that you initially run it without defining the serveroutput on. If the results of the two rounds.

    Published by: Lacotte Renaud on December 2, 2011 07:54

Maybe you are looking for

  • Clean install on Windows 7 SP1 x 64. Tools / Options-&gt; AppHangB1

    I use Firefox for years, but I can't find a way to work around this problem. The problem started after I upgraded to v38.0.5 but I tried to go back to v35.0 and it has not solved the problem. I tried the safe mode and finally uninstalled, delete Mozi

  • What do you call this window?

    Safari is not well fonctionnee this morning. After a few reboots, flushes the cache and choice swear words that my original problem has evaporated. But it does not lead to my current question: what we call this window? A few minutes ago, it was not s

  • error 7 GPIB / NOR-488

    Ladies and gentlemen, I am trying to contact a Keithley SMU 236 on Labview 2009 running on Windows XP. Write orders by VISA works very well, but written with functions GPIB does not work. I got following error messages: 7 Fehler ist bei GPIB: Schreib

  • It seems that my key cuser paste. It is a new computer Dell laptop should I call tech suport.

    need to call technical support. or return the laptop to QVC.com

  • Make a table in excel

    Hello guys,. Having a little trouble with my labview program. Very new to using the software so not quite sure about some things. The program, I built is designed to measure the deformation when a load is applied to a steel tube. The load on the stee