Need help with analytical function (LAG)

The requirement is as I have a table with described colums

col1 County flag Flag2

ABC 1 Y Y

XYZ 1 Y Y

XYZ 1 O NULL

xyz *2* N N

XYZ 2 Y NULL

DEF 1 Y Y

DEF 1 N NULL

To get the columns Flag2

1 assign falg2 as indicator for rownum = 1
2 check the colm1, count of current line with colm1, Earl of the previous line. The colm1 and the NTC are identical, should assign null...


Here's the query I used to get the values of Flag2


SELECT colm1, count, flag
BOX WHEN
LAG(Count, 1,null) OVER (PARTITION BY colm1 ORDER BY colm1 DESC NULLS LAST) IS NULL
and LAG(flag, 1, NULL) PLUS (SCORE FROM colm1 ORDER BY colm1, cycle DESC NULLS LAST) IS NULL
THEN the flag
END AS Flag2
FROM table1


but the query above returns the o/p below which is false

col1_ County flag Flag2

ABC 1 Y Y
XYZ 1 Y Y
XYZ 1 O NULL
xyz *2* N NULL
XYZ 2 Y NULL
DEF 1 Y Y
DEF 1 N NULL


Thank you

Published by: user9370033 on April 8, 2010 23:25

Well, you have not enough explained your full requirement in this

1 assign falg2 as indicator for rownum = 1
2 check the colm1, count of current line with colm1, Earl of the previous line. The colm1 and the NTC are identical, should assign null...

as you say not what Flag2 must be set on if com1 and cnt are not the same as the previous row.

But how about this as my first guess what you mean...

SQL> with t as (select 'abc' as col1, 1 as cnt, 'Y' as flag from dual union all
  2             select 'xyz', 1, 'Y' from dual union all
  3             select 'xyz', 1, 'Y' from dual union all
  4             select 'xyz', 2, 'N' from dual union all
  5             select 'xyz', 2, 'Y' from dual union all
  6             select 'def', 1, 'Y' from dual union all
  7             select 'def', 1, 'N' from dual)
  8  -- END OF TEST DATA
  9  select col1, cnt, flag
 10        ,case when lag(col1) over (order by col1, cnt) is null then flag
 11              when lag(col1) over (order by col1, cnt) = col1 and
 12                   lag(cnt) over (order by col1, cnt) = cnt then null
 13              else flag
 14         end as flag2
 15  from t
 16  /

COL        CNT F F
--- ---------- - -
abc          1 Y Y
def          1 Y Y
def          1 N
xyz          1 Y Y
xyz          1 Y
xyz          2 Y Y
xyz          2 N

7 rows selected.

SQL>

Tags: Database

Similar Questions

  • Need help with the function or metric derivative to calculate percentages of threshold for a measure

    Hi, first post to the community that I am a n00b Foglight needing help.

    A thing (in fact the only thing) I like Microsoft SCOM is how this graph of the availability of a metric, and I want to do the same thing in Foglight. I understand that this could be a derived measure or a function, I need, but am a bit lost right now.

    Let's say I have a metric and created thresholds as follows: normal included 0, 50 inclusive, warning critical 75 inclusive, fatal 100 inclusive + 9999 included. The metric is measured every so often and more often (99%) of the time it's normal. I want to visually represent that fact, together with the percentage of time that he spends in the strips of quick, critical and fatal alert threshold.

    For the dashboard but mainly reports I am looking for the percentage of time that the metric through each of the bands of threshold and put them in some form of chart, preferably very similar to how SCOM it: -.

    I would also like to increase this visually with a quantification for the oriented numercally, in order to insert values in the report for clarification would be great too, for example:-Normal:-99% 0.5% warning critical Fatal 0.1% 0.4%

    I think of what I have already learned that we have to include a 'blue' band for threshold indefinite in order to operate on a regular basis for any measure.

    I do not seem to come up with this concept in Foglight but I think it could be very useful to have something. Any help is most appreciated.

    Health and alarms is a standard display which can be used on any object topology.  You can access it from the data browser and should also be able to specify this dashboard as a preference in personalized dashboards.

    Here it is in a custom dashboard:

  • Need help with Group functions

    I'm a total novice with SQL, so please forgive me if the answer to my question seems to be too obvious
    I work with diagrams of the sample (in particular with the employees table):

    DESC employees;
    result

    What I have to do is select all the managers, including the number of subordinates is higher than the average number of subordinates of managers who work in the same Department. What I've done so far is as follows:

    SELECT mgr.employee_id manager_id, Director of mgr.last_name, mgr.department_id, COUNT (emp.employee_id)
    Employees emp employees JOIN Bishop
    ON emp.manager_id = mgr.employee_id
    GROUP OF mgr.employee_id, mgr.last_name, mgr.department_id
    ORDER BY mgr.department_id;
    result

    As you can see, I'm almost done. Now, I need only to calculate the average of the result of the COUNT function for each Department. But I'm totally stuck at this point.
    All advice?

    Hello

    Welcome to the forum!

    user12107811 wrote:
    I'm a total novice with SQL, so please forgive me if the answer to my question seems to be too obvious

    Just the opposite! Looks like a very difficult mission.

    I work with diagrams of the sample (in particular with the employees table):

    DESC employees;
    result

    What I have to do is select all the managers, including the number of subordinates is higher than the average number of subordinates of managers who work in the same Department. What I've done so far is as follows:

    SELECT mgr.employee_id manager_id, Director of mgr.last_name, mgr.department_id, COUNT (emp.employee_id)
    Employees emp employees JOIN Bishop
    ON emp.manager_id = mgr.employee_id
    GROUP OF mgr.employee_id, mgr.last_name, mgr.department_id
    ORDER BY mgr.department_id;
    result

    As you can see, I'm almost done. Now, I need only to calculate the average of the result of the COUNT function for each Department. But I'm totally stuck at this point.
    All advice?

    Yes, you're almost done. You just need to add one more condition. You have to calculate the average value of total_cnt (the COUNT (*) you already do) of a Department and compare that to total_cnt.

    There are several ways to do this, including
    a scalar subquery (in a HAVING clause)
    (b) make a result set with one line per Department, containing the average_cnt and reach than your current result set
    (c) analytical functions. Analytical functions are calculated after the GROUP BY clause is applied and aggregate functions are calculated, it is legitimate to say "AVG (COUNT (*)) MORE (...)").

    If thinking (c) is the simplest. It involves the use of a query of Tahina, but (a) and (b) also require subqueries.

    This sounds like homework, so I'll do it for you.
    Instead, here is a very similar problem with the hr.employees table.
    Let's say that we are interested in total wages given each type of work in each Department.

    SELECT        department_id
    ,        job_id
    ,        SUM (salary)     AS sum_sal
    FROM        hr.employees
    GROUP BY   department_id
    ,             job_id
    ORDER BY   department_id
    ,             job_id
    ;
    

    Results:

    DEPARTMENT_ID JOB_ID        SUM_SAL
    ------------- ---------- ----------
               10 AD_ASST          4400
               20 MK_MAN          13000
               20 MK_REP           6000
               30 PU_CLERK        13900
               30 PU_MAN          11000
               40 HR_REP           6500
               50 SH_CLERK        64300
               50 ST_CLERK        55700
               50 ST_MAN          36400
               60 IT_PROG         28800
               70 PR_REP          10000
               80 SA_MAN          61000
               80 SA_REP         243500
               90 AD_PRES         24000
               90 AD_VP           34000
              100 FI_ACCOUNT      39600
              100 FI_MGR          12000
              110 AC_ACCOUNT       8300
              110 AC_MGR          12000
                  SA_REP           7000
    

    Now suppose we want to find out which of these sum_sals is higher than the average sum_sal of his Department.
    For example, in detriment 110 (near the end OIF the list) there two types of work (AC_ACCOUND and AC_MGR) that have sum_sals of 8300 and 12000. The average of these two numbers is 10150, so we selected AC_MGR (because its sum_sal, 12000, is superior to 10150, and we do not want to include AC_ACCOUNT, because its sum_sal, 8300, is less than or equal to the average of the Department.
    In departments where there is only one job type (for example, Department 70, or null "Department" at the end of the list above) the only sum_sal will be the average; and because the sum_sal is not greater than the average, we want to exclude this line.

    Let's start with the calculation of the avg_sum_sal using the analytical function AVG:

    SELECT        department_id
    ,        job_id
    ,        SUM (salary)                              AS sum_sal
    ,        AVG (SUM (salary)) OVER (PARTITION BY department_id)     AS avg_sum_sal
    FROM        hr.employees
    GROUP BY   department_id
    ,             job_id
    ORDER BY   department_id
    ,             job_id
    ;
    

    Output:

    DEPARTMENT_ID JOB_ID        SUM_SAL AVG_SUM_SAL
    ------------- ---------- ---------- -----------
               10 AD_ASST          4400        4400
               20 MK_MAN          13000        9500
               20 MK_REP           6000        9500
               30 PU_CLERK        13900       12450
               30 PU_MAN          11000       12450
               40 HR_REP           6500        6500
               50 SH_CLERK        64300  52133.3333
               50 ST_CLERK        55700  52133.3333
               50 ST_MAN          36400  52133.3333
               60 IT_PROG         28800       28800
               70 PR_REP          10000       10000
               80 SA_MAN          61000      152250
               80 SA_REP         243500      152250
               90 AD_PRES         24000       29000
               90 AD_VP           34000       29000
              100 FI_ACCOUNT      39600       25800
              100 FI_MGR          12000       25800
              110 AC_ACCOUNT       8300       10150
              110 AC_MGR          12000       10150
                  SA_REP           7000        7000
    

    Now all we have to do is to compare the sum_sal and avg_sum_sal columns.
    Given that the analytic functions are calculated after the WHERE clause is applied, we cannot use avg_sum_sal in the WHERE clause of the query, even where it has been calculated. But we can do that in a subquery; Then, we can use avg_sum_sal in any way that we love in the Super-requete:

    WITH     got_avg_sum_sal       AS
    (
         SELECT        department_id
         ,        job_id
         ,        SUM (salary)                              AS sum_sal
         ,        AVG (SUM (salary)) OVER (PARTITION BY department_id)     AS avg_sum_sal
         FROM        hr.employees
         GROUP BY   department_id
         ,             job_id
    )
    SELECT        department_id
    ,        job_id
    ,        sum_sal
    FROM        got_avg_sum_sal
    WHERE        sum_sal     > avg_sum_sal
    ORDER BY   department_id
    ,             job_id
    ;
    

    Results:

    DEPARTMENT_ID JOB_ID        SUM_SAL
    ------------- ---------- ----------
               20 MK_MAN          13000
               30 PU_CLERK        13900
               50 SH_CLERK        64300
               50 ST_CLERK        55700
               80 SA_REP         243500
               90 AD_VP           34000
              100 FI_ACCOUNT      39600
              110 AC_MGR          12000
    
  • More help with analytical functions

    I had great hellp here yesterday and I need once more today. I guess I'm still not able to get a solid understanding of analytical functions. So here's the problem:
    table with 3 collars:
    product_id (int), sale_date (to date), count_sold (int) - each file show that the number of items have been sold for the product at a given date.
    The query should return the 3 passes of the table AND a fourth column that contains the date with the best sales of the product. If there are two or more dates with equal sales, the last being is chosen.

    Is this possible using an analytical function appropriately and without using a subquery?

    example:
    product_id, sale_date, count_sold, high_sales_date
    1, 01-01-2008, 10, 05/10/2008,.
    1, 2008-03-10, 20, 10/05/2008
    1, 10/04/2008, 25, 05/10/2008
    1, 10/05/2008, 25, 05/10/2008
    1, 01/06/2008, 22, 05/10/2008
    2, 05/12/2008, 12, 05/12/2008
    2, 06/01/2009, 10, 05/12/2008

    Thank you

    Hello

    Try this:

    SELECT     product_id
    ,     sale_date
    ,     count_sold
    ,     FIRST_VALUE (sale_date) OVER ( PARTITION BY  product_id
                                   ORDER BY          count_sold          DESC
                               ,               sale_date          DESC
                             )      AS high_sales_date
    FROM     table_x;
    

    If you would post INSERT statements for your data, then I could test it.

    Focus issue: Why use FIRST_VALUE with descending order and not LAST_VALUE (ASCending) ORDER of default?

  • Need help with the function

    First off the coast to let put me the warning that I am not the right Wick informed in PL/SQL. I use this for an example posted in the forum of the APEX.

    I need the function to return values based on the user role. I know, it's probably the ugliest pl/sql, you may have seen yet ;) I hope that you guys can decipher the logic that I use. Obviously, this code does not validate.

    You guys could help me with this?

    FUNCTION to CREATE or REPLACE return_art_lov_fn
    RETURN art_table_type
    AS
    v_data art_table_type: = art_table_type ();
    BEGIN
    IF
    EXISTS (SELECT ' 1' FROM GBL_PEOPLE, GBL_ACCESS WHERE upper (gbl_people.userid) = upper (app_user) AND gbl_people.person_id = gbl_access.person_id and gbl_access.art_role = 9)
    THEN
    FOR c IN (SELECT reverse_name, person_id
    OF gbl_people)
    LOOP
    v_data. EXTEND;
    v_data (v_data. (COUNTY): = art_rectype (c.person_id, c.reverse_name);
    END LOOP;
    ON THE OTHER
    FOR c IN (select reverse_name, person_id of GBL_PEOPLE where upper (userid) = upper (APP_USER) and current_flag is not null
    Union
    Select reverse_name, person_id from GBL_PEOPLE where mgr_person_id = (select person_id in GBL_PEOPLE where upper (userid) = upper (APP_USER) and)
    (current_flag is not null) and current_flag is not null)
    LOOP
    v_data. EXTEND;
    v_data (v_data. (COUNTY): = art_rectype (c.person_id, c.reverse_name);
    END LOOP;
    ENDIF;
    RETURN v_data;
    END;

    -Vinod

    My guess is that you want something like

    CREATE OR REPLACE FUNCTION return_art_lov_fn
      RETURN art_table_type
    AS
      v_data art_table_type;
      l_cnt    integer;
    BEGIN
      SELECT 1
        INTO l_cnt
        FROM dual
       WHERE EXISTS(
          SELECT 1
            FROM gbl_people p,
                 gbl_access a
           WHERE upper( p.userid ) = upper( app_user )
             AND p.person_id       = a.person_id
             AND a.art_role        = 9
       );
    
      IF( l_cnt = 1 )
      THEN
        SELECT art_rectype( person_id, reverse_name )
          BULK COLLECT INTO v_data
          FROM gbl_people;
      ELSE
        SELECT art_rectype( person_id, reverse_name )
          BULK COLLECT INTO v_data
          FROM (SELECT person_id, reverse_name
                  FROM gbl_people
                 WHERE upper(userid) = upper(app_user)
                   AND current_flag is not null
                UNION
                SELECT person_id, reverse_name
                  FROM gbl_people
                 WHERE mgr_person_id = (SELECT person_id
                                          FROM gbl_people
                                         WHERE upper(userid) = upper( app_user )
                                           AND current_flag is not null)
                   AND current_flag is not null);
      END IF;
    
      RETURN v_data;
    END;
    

    If this does not work (I do not have definitions table, so I can't try to compile it myself), please report all errors of compilation (including line numbers).

    Justin

  • Help with analytical functions

    Hi all

    I'm on Oracle 11g DB and have records in the table that look like this
    transaction_ref   line_type   description
    --------------------   --------------  ---------------
     10                   DETAIL      abc123          
     10                   DETAIL      abc978          
     10                   DETAIL      test              
     10                   DETAIL      test              
     10                   DETAIL      test              
     20                   DETAIL      abcy             
     20                   DETAIL      abc9782       
     20                   DETAIL      test12          
     20                   DETAIL      test32          
    Analytical, I generate rownumber by Ref single transaction as follows:
    SELECT row_number() over (partition by transaction_ref order by 1) rownumber
    FROM mytable ;
    
    
    transaction_ref   line_type   description   rownumber
    --------------------   --------------  ---------------   ----------------
     10                   DETAIL      abc123          1
     10                   DETAIL      abc978          2
     10                   DETAIL      test              3
     10                   DETAIL      test              4
     10                   DETAIL      test              5
     20                   DETAIL      abcy             1
     20                   DETAIL      abc9782       2
     20                   DETAIL      test12          3
     20                   DETAIL      test32          4
    However, for my needs, I need my rownumber as follows:

    with the exception of number 1 of Clotilde, I want to increment the number of lines per 3
     transaction_ref   line_type   description   rownumber
    --------------------   --------------  ---------------   ----------------
     10                   DETAIL      abc123          1
     10                   DETAIL      abc978          4
     10                   DETAIL      test              7
     10                   DETAIL      test              10
     10                   DETAIL      test              13
     20                   DETAIL      abcy             1
     20                   DETAIL      abc9782       4
     20                   DETAIL      test12          7
     20                   DETAIL      test32          10
    .... 
    Thank you
    Maëlle

    Published by: user565538 on June 4, 2011 17:32

    Published by: user565538 on June 4, 2011 17:34

    Published by: user565538 on June 4, 2011 17:35
    with mytable as (
                     select 10 transaction_ref,'DETAIL' line_type,'abc123' description from dual union all
                     select 10,'DETAIL','abc978' from dual union all
                     select 10,'DETAIL','test' from dual union all
                     select 10,'DETAIL','test' from dual union all
                     select 10,'DETAIL','test' from dual union all
                     select 20,'DETAIL','abcy' from dual union all
                     select 20,'DETAIL','abc9782' from dual union all
                     select 20,'DETAIL','test12' from dual union all
                     select 20,'DETAIL','test32' from dual
                    )
    SELECT  transaction_ref,
            line_type,
            description,
            (row_number() over (partition by transaction_ref order by 1) - 1) * 3 + 1 rownumber
    FROM mytable
    /
    
    TRANSACTION_REF LINE_T DESCRIP  ROWNUMBER
    --------------- ------ ------- ----------
                 10 DETAIL abc123           1
                 10 DETAIL abc978           4
                 10 DETAIL test             7
                 10 DETAIL test            10
                 10 DETAIL test            13
                 20 DETAIL abcy             1
                 20 DETAIL abc9782          4
                 20 DETAIL test12           7
                 20 DETAIL test32          10
    
    9 rows selected.
    
    SQL> 
    

    SY.

  • Need help with a function

    Oracle 9i

    The tables are
    CREATE TABLE VEHICLE_CLASS(VEHICLE_CLASS CHAR(2),CAR_LD VARCHAR2(50));
    
    INSERT INTO vehicle_class VALUES('K','130 STANDARD');
    
    CREATE TABLE VEHICLE_BODY(V_BODY CHAR(2),CAR_LD VARCHAR2(50));
    
    INSERT INTO vehicle_body VALUES('B','3 DOOR STATION WAGON');
    The function is respectively
    CREATE FUNCTION V_CLASS(V_IN VARCHAR2) RETURN VARCHAR2
    AS
         veh_type           VARCHAR2(20):=substr(v_in,1,2);
             veh_class           VARCHAR2(20):=substr(v_in,3,1);
             veh_body            VARCHAR2(20):=SUBSTR(v_in,4,1);
             veh_rtn             varchar2(50);
             vehicle_found           char;
             VEHICLE_BODY_FOUND      CHAR;
    BEGIN
    IF      veh_type='LD'
    THEN
    DBMS_OUTPUT.PUT_LINE('FOUND');
    SELECT      'y' INTO vehicle_found
    FROM      vehicle_class
    WHERE      v_class=veh_class
    AND      defender_ld_desc IS NOT NULL;
    DBMS_OUTPUT.PUT_LINE('FOUND2');
    SELECT      'y' INTO VEHICLE_BODY_FOUND
    FROM      vehicle_body
    WHERE      v_body=veh_body
    AND      defender_ld_desc IS NOT NULL;
    DBMS_OUTPUT.PUT_LINE('FOUND 3');
    IF      vehicle_found='y'
    THEN
    IF VEHICLE_BODY_FOUND='y'
    THEN
    veh_rtn     := 'VALID';
    ELSE
    veh_rtn     := 'CAR - INVALID BODY STYLE CODE';
    END IF;
    ELSE
    veh_rtn     := 'CAR - INVALID CLASS CODE';
    END IF;
    END IF;
    RETURN      veh_rtn;
    EXCEPTION
    WHEN      NO_DATA_FOUND
    THEN
    RETURN      'INVALID';
    WHEN OTHERS
    THEN
    RETURN      'OTHER PROBLEM';
    END      V_CLASS;
    ENTER PAST

    'LDKB '.



    THE OUTPUT IS
    LDKB
    FOUND
    INVALID
    Kindly help me as to why the function returns invalid, it is not in the select statement, even if the value is valid.

    The problem is in your definition of the table...

    CREATE TABLE VEHICLE_CLASS(VEHICLE_CLASS CHAR(2),CAR_LD VARCHAR2(50));
    
    INSERT INTO vehicle_class VALUES('K','130 STANDARD');
    
    CREATE TABLE VEHICLE_BODY(V_BODY CHAR(2),CAR_LD VARCHAR2(50));
    
    INSERT INTO vehicle_body VALUES('B','3 DOOR STATION WAGON');
    

    Your vehicle_class and v_body are defined as CHAR (2) so when you insert the value 'B' and 'K' in those he's filling them with spaces to 2 characters.

    Best bet is to avoid using TANK in your table definitions, unless you use only single character flags (for example, CHAR (1)) in which case it will not have adverse and simply stick to use instead of VARCHAR2.

  • I have a HP G60 554 CA most of the time of love - need help with the functions of the keyboard.

    On my HP G60 554 CA, I do a lot of accounting/bookkeeping.  Suddenly, I have no idea what I did but my alphabet keyboard turns suddenly to the BLUE character on the key of the alphabet.  For example - the question mark & / key (white color) is equivalent to using the blue French E and e I know it's to do with the FN or fn key but I don't know how to tell Bank to regular.    Any help would be appreciated.

    Thank you.

    Take a look at this link

    Why should I hold down the "Fn" key to get normal letter?

  • Help with analytical functions - Windowing

    Hello

    I'm using Oracle 11.2.0.4.0.

    I want to do the sum of all amounts for each window of 3 days from the date of the oldest rolling.  I also want to name each window with the date limit for the period of 3 days.

    My requirement is slightly more complicated, but I use this example to illustrate what I'm trying to

    create table test (dt date, amt, run_id number);

    Insert test values (to_date (' 22/04/2015 ',' dd/mm/yyyy'), 1, 1);

    Insert test values (to_date (' 23/04/2015 ',' dd/mm/yyyy'), 1, 1);

    Insert test values (to_date (' 24/04/2015 ',' dd/mm/yyyy'), 1, 1);

    Insert test values (to_date (' 25/04/2015 ',' dd/mm/yyyy'), 1, 1);

    Insert test values (to_date (' 27/04/2015 ',' dd/mm/yyyy'), 5, 1);

    Insert test values (to_date (' 28/04/2015 ',' dd/mm/yyyy'), 2, 1);

    Insert test values (to_date (' 29/04/2015 ',' dd/mm/yyyy'), 1, 1);

    Insert test values (to_date (' 04/30/2015 ',' dd/mm/yyyy'), 1, 1);

    Insert test values (to_date (' 01/05/2015 ',' dd/mm/yyyy'), 1, 1);

    Insert test values (to_date (' 02/05/2015 ',' dd/mm/yyyy'), 1, 1);

    Insert test values (to_date (' 03/05/2015 ',' dd/mm/yyyy'), 1, 1);

    Insert test values (to_date (' 04/05/2015 ',' dd/mm/yyyy'), 1, 1);

    The output should look like the example below.  The period column requires

    to show the end of each 3-day study:

    AMT DT SUM_PER_PERIOD PERIOD

    22/04/2015 1 1 24/04/2015

    23/04/2015 1 2 24/04/2015

    24/04/2015 1 3 24/04/2015

    25/04/2015 1 3 27/04/2015

    27/04/2015 5 6 27/04/2015

    28/04/2015 2 7 30/04/2015

    29/04/2015 20 27 30/04/2015

    30/04/2015 30 52 30/04/2015

    05/01/2015 5 55 3/05/2015

    05/02/2015 5 50 3/05/2015

    05/02/2015 10 50 3/05/2015

    05/03/2015 1 21/3/05/2015

    All I can manage this is

    Select dt

    TN

    , sum (amt) on sum_per_period (PARTITION BY run_id ORDER BY dt vary from 2 PAST current line)

    of the test

    order by dt;

    Can anyone help?

    It's very kind of you to give the insert and create instructions... but I corrected the data a bit

    It does not match the output see you below

    starting from 29/04, you forgot to change the dates and numbers of...

    insert into test values (to_date('22/04/2015','dd/mm/yyyy'),1,1);
    insert into test values (to_date('23/04/2015','dd/mm/yyyy'),1,1);
    insert into test values (to_date('24/04/2015','dd/mm/yyyy'),1,1);
    insert into test values (to_date('25/04/2015','dd/mm/yyyy'),1,1);
    insert into test values (to_date('27/04/2015','dd/mm/yyyy'),5,1);
    insert into test values (to_date('28/04/2015','dd/mm/yyyy'),2,1);
    insert into test values (to_date('29/04/2015','dd/mm/yyyy'),20,1);
    insert into test values (to_date('30/04/2015','dd/mm/yyyy'),30,1);
    insert into test values (to_date('01/05/2015','dd/mm/yyyy'),5,1);
    insert into test values (to_date('02/05/2015','dd/mm/yyyy'),5,1);
    insert into test values (to_date('02/05/2015','dd/mm/yyyy'),10,1);
    insert into test values (to_date('03/05/2015','dd/mm/yyyy'),1,1);
    

    your periods will change if you insert a new first date...

    so I guess you want a specific date... in this case 22/04/2015 and a specific end date

    creation of periods from this first date and then grouping of these periods is easier with a first fixed date and a delta of 3 days.

    the first step is to match the periods to your data (adapted)

    with periods as (
      select date_start + (level-1) * period_days period_start, date_start + level * period_days period_end, period_days from (
        select to_date('21/04/2015', 'dd/mm/yyyy') date_start, to_date('04/05/2015', 'dd/mm/yyyy') date_end, 3 period_days   from dual)
      connect by date_start + level * period_days  < date_end)
    select *
    from test t, periods p
    where t.dt > p.period_start and t.dt <= p.period_end
    

    This gives your data with the dates of beginning and ending period

    DT AMT RUN_ID PERIOD_START PERIOD_END PERIOD_DAYS
    22/04/2015

    1

    1

    21/04/2015 24/04/2015

    3

    23/04/2015

    1

    1

    21/04/2015 24/04/2015

    3

    24/04/2015

    1

    1

    21/04/2015 24/04/2015

    3

    25/04/2015

    1

    1

    24/04/2015 27/04/2015

    3

    27/04/2015

    5

    1

    24/04/2015 27/04/2015

    3

    28/04/2015

    2

    1

    27/04/2015 30/04/2015

    3

    29/04/2015

    20

    1

    27/04/2015 30/04/2015

    3

    30/04/2015

    30

    1

    27/04/2015 30/04/2015

    3

    05/01/2015

    5

    1

    30/04/2015 05/03/2015

    3

    05/02/2015

    5

    1

    30/04/2015 05/03/2015

    3

    05/02/2015

    10

    1

    30/04/2015 05/03/2015

    3

    05/03/2015

    1

    1

    30/04/2015 05/03/2015

    3

    and then sum the amt during the 3 days

    with periods as (
      select date_start + (level-1) * period_days period_start, date_start + level * period_days period_end, period_days from (
        select to_date('21/04/2015', 'dd/mm/yyyy') date_start, to_date('04/05/2015', 'dd/mm/yyyy') date_end, 3 period_days   from dual)
      connect by date_start + level * period_days  < date_end)
    select t.dt, t.amt, sum(amt) over (order by t.dt range between 2 preceding and current row) sum_per_period, p.period_end period
    from test t, periods p
    where t.dt > p.period_start and t.dt <= p.period_end
    

    giving your output as requested:

    DT AMT SUM_PER_PERIOD PERIOD
    22/04/2015

    1

    1

    24/04/2015
    23/04/2015

    1

    2

    24/04/2015
    24/04/2015

    1

    3

    24/04/2015
    25/04/2015

    1

    3

    27/04/2015
    27/04/2015

    5

    6

    27/04/2015
    28/04/2015

    2

    7

    30/04/2015
    29/04/2015

    20

    27

    30/04/2015
    30/04/2015

    30

    52

    30/04/2015
    05/01/2015

    5

    55

    05/03/2015
    05/02/2015

    5

    50

    05/03/2015
    05/02/2015

    10

    50

    05/03/2015
    05/03/2015

    1

    21

    05/03/2015
  • Need help with the function MAX Returns the values

    I am creating a report to return slow movement inventory data. One of the requests is that it return only the last date that an item is traded on. A map shows the last date of receipt for a part, the other will show the last time, a part has been issued or shipped on a sales order.

    The hiccup is that it returns every single of the last time that an item has been received, so every last show of the material (on the second sheet) of items on-site.

    Could someone help me set the max value? As shown below, and many variations, the sheet comes up with no corrupt data or dates.

    MAX (Date of the MAX operation) (PARTITION OF matter Transactions.Item matter Transactions.Item ORDER)

    Always returns the next two when in reality, I want just the one with the date the most recent (April 2010).

    100034 BNDSCE-105 - QUALITY BEARINGS OR EQUIVALENT A400M AB01D...     Problem of component WIP $0.00 11-Sep-2009-3
    100034 BNDSCE-105 - QUALITY BEARINGS OR EQUIVALENT A400M AD01D...     Problem of component WIP $0.00 April 13, 2010-16

    Thank you for your help.

    Becka

    Hi Becka
    If you have a new calculated column showng, as in your example, 13 April 10, and you have another column that contains also this date, you should be able to create a new condition. Condition that must be the date calculated max is equal to the actual date, thus returning only the line that includes 13 April 10.

    I hope I'm makng sense

    Best wishes
    Michael

  • Need help with to_char function

    Hi all

    I use sql query below to get sysdate with timestamp.

    Select to_char (' sysdate, ' YYYYMMDD HH24:MM:SS ") of the double

    It gives me below the result: 20130816 05:08:49

    Strangely, when I keep on run the same query, I noticed after 60 seconds are completed, timestamp begins with 20130816 05:08:01 again.

    I'm missing something here...

    Thanks in advance

    998158 wrote:

    Select to_char (' sysdate, ' YYYYMMDD HH24:MM:SS ") of the double

    Strangely, when I keep on run the same query, I noticed after 60 seconds are completed, timestamp begins with 20130816 05:08:01 again.

    Hello

    You use Mi minutes not MM.

    Use like this

    Select to_char (sysdate, "YYYY/MM/DD HH24:Mi:SS) twice;

    See you soon!

  • Simple problem: need help with string functions

    I'm sure it's easy, but it's late and I'm tired.

    If I had a string that contained a full name for example.
    < cfset name = 'John Smith' >

    How would manipulate the string so I could separate in two variables for example.
    FirstName = 'John '.
    LastName = 'Smith '.

    See you soon

    Swampie says:
    > How would manipulate the string so I could separate in two variables for example.
    > firstname = 'John '.
    > lastname = 'Smith '.

    pretend it's a list delimited by spaces.

    name = 'Jane Doe';
    Switch (listLen (name,"" ")) {}
    case 1:
    manage single names
    break;
    case 2:
    firstName = listFirst (name,"" ");
    lastName = listLast (name,"" ");
    break;
    case 3:
    firstName = listFirst (name,"" ");
    middleName = listGetAt ("name, 2," "");
    lastName = listLast (name,"" ");
    break;
    } / / switch

    probably need a break by default for > 3 names.

  • Help with analytic function

    version 9.2

    Here is a sample
    WITH temp AS
         (SELECT 10 ID, TRUNC (SYSDATE - 1) dt, 101 ord_id
            FROM DUAL
          UNION
          SELECT 11 ID, TRUNC (SYSDATE - 1) dt, 101 ord_id
            FROM DUAL
          UNION
          SELECT 11 ID, TRUNC (SYSDATE) dt, 103 ord_id
            FROM DUAL
          UNION
          SELECT 13 ID, TRUNC (SYSDATE) dt, 104 ord_id
            FROM DUAL)
    SELECT *
      FROM temp
    Output: number of separate orders for each date
    Dt     Count
    1/25  1
    1/26  2
    ME_XE?WITH temp AS  2       (SELECT 10 ID, TRUNC (SYSDATE - 1) dt, 101 ord_id  3          FROM DUAL  4        UNION  5        SELECT 11 ID, TRUNC (SYSDATE - 1) dt, 101 ord_id  6          FROM DUAL  7        UNION  8        SELECT 11 ID, TRUNC (SYSDATE) dt, 103 ord_id  9          FROM DUAL 10        UNION 11        SELECT 13 ID, TRUNC (SYSDATE) dt, 104 ord_id 12          FROM DUAL) 13  SELECT dt, count(distinct ord_id) 14  FROM temp 15  group by dt;
    
    DT                         COUNT(DISTINCTORD_ID)-------------------------- ---------------------25-JAN-2009 12 00:00                           126-JAN-2009 12 00:00                           2
    
    2 rows selected.
    
    Elapsed: 00:00:00.01ME_XE?ME_XE?
    
  • Need help with a SQL query

    Hello

    I have a data in table (raj_table) with columns (char11) raj_id, raj_number (varchar2 (15)), raj_format (NUMBER), Primary_ID (identity with the values of the primary key column)

    Primary_ID raj_id Raj_number Raj_format

    1                            raj                 rajvend                      1

    2                            raj                 rajvend                      1

    3                            raj                 rajvendor1                 2

    4                            raj                 rajvendor1                 2

    5                            raj                 rajvendor1                 2

    6                            raj                 rajvendor2                 3

    I used under SQL to get query output as below, but has not achieved the required result:

    Select client_id vendor_number, vendor_format, primary_id, row_number() on sl_no (client_id partition, primary_id, vendor_format order of client_id primary_id, vendor_format, vendor_number, vendor_number)

    from raj_table by sl_no asc

    SL_NO raj_id raj_number raj_format primary_id

    1                   1                   raj              rajvendor                 1

    1                   2                  raj              rajvendor                 1

    2                   3                   raj              rajvendor1                2

    2                   4                   raj              rajvendor1                2

    2                   5                  raj               rajvendor1                2

    3                   6                    raj              rajvendor2                3

    I need help with a SQL query to get the result as above without using the group by clause. I want to bring together the combination of separate line of the three columns (raj_id, raj_number, raj_format) and add a unique serial number for each online game (SL_NO column below). So, above there are 3 unique set of (raj_id, raj_number, raj_format) I can get in a group by clause, but I can not add prmiary_id, SL_NO values if I group by clause. I used the analytical functions like row_number() but no luck. Need solution for this.

    with t as)

    Select 'raj' raj_id, 'rajvend' raj_number, 1 raj_format, 1 primary_id Union double all the

    Select option 2, 'raj', 'rajvend', 1 double Union all

    Select 3, 'raj', 'rajvendor1', 2 double Union all

    Select 4, 'raj', 'rajvendor1', 2 double Union all

    Select 5, 'raj', 'rajvendor1', 2 double Union all

    Select 6, 'raj', 'rajvendor2', 3 double

    )

    Select dense_rank() over (order of raj_id, raj_number, raj_format) sl_no,

    t.*

    t

    order by primary_id

    /

    PRIMARY_ID RAJ RAJ_NUMBER RAJ_FORMAT SL_NO
    ---------- ---------- --- ---------- ----------
    1 1 raj rajvend 1
    1 2 raj rajvend 1
    2 3 raj rajvendor1 2
    2 4 raj rajvendor1 2
    2 5 raj rajvendor1 2
    3 6 raj rajvendor2 3

    6 selected lines.

    SQL >

    SY.

  • I'm suddenly needing help with my browser Firefox (6.0.2)

    Hello
    I'm suddenly needing help with my browser Firefox (6.0.2)

    (OS: I use Windows XP).

    When I open the browser, I don't see is a totally white screen of white, with all the toolbars at the top.

    I know that my physical connections are very good: I have tested the modem, turned the pc market etc and I can also receive/send emails.

    This problem started today, September 8, 2011 and has never happened before.

    Is it a coincidence that Firefox itself to day before I disconnected yesterday evening? Could this be something to do with this particular new update?

    I also noticed that just before I "opened" Firefox, I now get a small box indicating:

    [JAVASCRIPT APPLICATION]
    Handl exc in Ev: TypeError: this oRoot.enable is not a function

    This never appeared before - I hope that it offers a clue has what is wrong.

    The browser not be stuck in Mode safe, said by the way.

    Of course, I can't find solutions to the problem on the internet, I don't physically see all Web sites!
    (A friend sends this request in my name from their pc)

    Any light you can throw on this problem of confusion would be much appreciated. I'd rather not have to uninstall and reinstall Firefox if possible.

    If the only option is to uninstall Firefox and reinstall from your site, I'm also in trouble (I can not see the internet or download).
    In this case, would you be able to send the .exe file as an attachment to my e-mail address? In the affirmative, please let me know and I'll give you more details.

    Thanks in advance.

    One possible cause is security software (firewall) that blocks or limits Firefox or plugin-container process without informing you, possibly after the detection of changes (update) for the Firefox program.

    Delete all rules for Firefox in the list of permissions in the firewall and leave your firewall again ask permission to get full unlimited access to the internet for Firefox and the plugin-container and the update process.

    See:

    Start Firefox in Firefox to solve the issues in Safe Mode to check if one of the extensions of the origin of the problem (switch to the DEFAULT theme: Firefox (Tools) > Add-ons > appearance/themes).

Maybe you are looking for

  • The MIG 229771 in c# software error

    Hello I use an NI USB-6009 data acquisition. I'm unable to create a task using c#, I get a MIG 229771 software error when I try to instantiate with 'myTask1 task = new Task();' or 'task myTask1 = DaqSystem.Local.LoadTask ("D00");' but not when I crea

  • Lagg while streaming video

    Hi guys,.my video plays really lag then I play my laptop, but if I use it for my ipad is not... so it means there is sumthing wrong with my browsers or sumthing. can you guys tell me the solution for this please?

  • Stall network detection

    Currently, I'm trying to understand how to implement a network dropout detector in my application.  I use HTTP and so often keep a socket opened for a long time and do a lot of things on it.  Therefore, a random network dropping out really hurts to e

  • Use of automatic start and RuntimeStore for UIApplication... Help!

    I have a UIApplication that runs in the background and uses another entry point. The problem I have is to initialize the high device. The application will be auto-start and being thrust into the RuntimeStore (it seems anyway), and it will run in the

  • BlackBerry Smartphones shipper receiver postmaster message - not delivered on my handheld

    Hello, I recently canceled my blackberry phone, and I'm syncing my @me.com email with mobile e-mail account @mobileemail.vodafone.com.au. All of you who sent me an email to my account @me.com receive an email back saying that I him have not received