The appropriate to a MAX on an analytical function syntax

I am trying to apply a MAX function on a function analytically than doing a MAX as well. However, I can't understand the appropriate syntax. That's what I tried:
SELECT MAX(MAX(t.col1) OVER (PARTITION BY t.col2) AS max_col
  FROM sample_table t;
When I try to run the above query, I get a 't.col1 is invalid in the select list because it is not contained in an aggregate function or the GROUP BY clause error.

You must encapsulate the query that has the analytical function in another query and then make the max in the outer query.

Tags: Database

Similar Questions

  • Max date in analytic function

    I have records that a load dates repeat.
    I would like to return records with the maximum load_dates.

    My data source looks like this-
    (select 60589 as C_number, to_date('01/08/2012','DD/MM/YYYY') as load_dt of all the double union)
    Select 60768, to_date('01/08/2012','DD/MM/YYYY') of all the double union
    Select 60888, to_date('01/08/2012','DD/MM/YYYY') of all the double union
    Select 12345, to_date('01/09/2012','DD/MM/YYYY') of all the double union
    Select 54321, to_date('01/09/2012','DD/MM/YYYY') of all the double union
    Select 66666, to_date('01/10/2012','DD/MM/YYYY') of all the double union
    Select 55555, double to_date('01/10/2012','DD/MM/YYYY'))
    ;


    I would like to return records with the max load_dt which means
    C_number load_dt
    666666 1 October 12
    555555 1 October 12


    I wrote an analytical function of the oracle, but it does not work as it should be-
    My query looks like this-

    Select a.*
    Of
    (
    Select
    c_number,
    load_dt,
    Max (load_dt) more (load_dt partition) as mx_dt
    from table_name
    )
    where
    load_dt = mx_dt;

    It returns all the lines for some reason any.

    Any help or advice is much appreciated
    PJ
    max(load_dt) over (partition by load_dt) as mx_dt
    

    should be

    max(load_dt) over () as mx_dt
    

    If you want the maximum all the records, otherwise your was just the maximum per load_dt partition (think of partitions as being groups of data)

  • Unable to get the database searched with MAX and GROUP BY function

    Hello
    All the

    I have a table as below:
    COLUMN TYPE
    User_id VARCHAR2 (10 byte)
    ID_processus VARCHAR2 (30 bytes)
    END_TIME DATE (STAMP)
    TO_LOC VARCHAR2 (12 bytes)
    TO_LOC_TYPE VARCHAR2 (15 bytes)
    FROM_LOC VARCHAR2 (12 bytes)
    ITEM_ID VARCHAR2 (25 bytes)
    CASE NUMBER (12.4)
    LMS_UDA1 VARCHAR2 (250 bytes)
    AREA VARCHAR2 (2 bytes)

    I only want to get one record with all the columns, have only one clause MAX (END_TIME)
    But the other column value of the difference.
    When I use MAX (END_TIME) and GROUP OF USER_ID, ID_processus, CASE...
    the sql did not a single record,
    It gives the number of records

    Please help me on this

    Concerning

    Saven

    >
    I only want to get one record with all the columns, have only one clause MAX (END_TIME)
    But the other column value of the difference.
    >
    Maybe it's not possible because it depends on your data.

    If the combination of all columns except END_TIME is not unique then there will be multiple records; a record for each unique combination (GROUP BY) of all other columns.

    The only way to ensure that a record is

    SELECT MAX(END_TIME) FROM myTable
    
  • Purpose of the ORDER BY clause in the analytic function Min Max

    I was always using analytical functions like Min Max without ORDER BY clause. But today I used with the ORDER BY clause. The results are very different. I would like to know the purpose of the ORDER BY clause in Min, Max and analogues of analytical functions.

    user10566312 wrote:
    I was always using analytical functions like Min Max without ORDER BY clause. But today I used with the ORDER BY clause. The results are very different. I would like to know the purpose of the ORDER BY clause in Min, Max and analogues of analytical functions.

    It is a good point that many developers are not so aware. As far as I understand it the way it works.

    Some analytical functions do not need an order by or windowing clause (SUM, COUNT, MIN, etc.). If there is no specified window, then the full score is the window.
    As soon as you add a command also add you a windowing clause. This window has the default value of 'rank ofrows between unbounded preceding and current_row. So as soon as you add an order by clause, you get a sliding window.

    Documentation: http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions001.htm

    windowing_clause
    ...
    You cannot specify this clause unless you specified the order_by_clause. Window limits defined by the clause RANGE you can not specify only a single expression to the > order_by_clause. Please refer to 'Restrictions on the ORDER BY Clause'.

    example of

    with testdata as (select 10 numval, level lv from dual connect by level < 10)
    select lv, numval, sum(numval) over () sum1, sum(numval) over (order by lv) sum2
    from testdata;
    
    LV NUMVAL SUM1 SUM2
    -- ------ ---- ----
     1     10   90   10
     2     10   90   20
     3     10   90   30
     4     10   90   40
     5     10   90   50
     6     10   90   60
     7     10   90   70
     8     10   90   80
     9     10   90   90 
    

    Published by: Sven w. on 25 Sep 2012 16:57 - default behavior has been corrected. Thanks to Chris

  • A question about the analytical function used with the GROUP BY clause in SHORT

    Hi all

    I created the following table named myenterprise
    CITY       STOREID    MONTH_NAME TOTAL_SALES            
    ---------- ---------- ---------- ---------------------- 
    paris      id1        January    1000                   
    paris      id1        March      7000                   
    paris      id1        April      2000                   
    paris      id2        November   2000                   
    paris      id3        January    5000                   
    london     id4        Janaury    3000                   
    london     id4        August     6000                   
    london     id5        September  500                    
    london     id5        November   1000
    If I want to find which is the total sales by city? I'll run the following query
    SELECT city, SUM(total_sales) AS TOTAL_SALES_PER_CITY
    FROM myenterprise
    GROUP BY city
    ORDER BY city, TOTAL_SALES_PER_CITY;
    that works very well and produces the expected result, i.e.
    CITY       TOTAL_SALES_PER_CITY   
    ---------- ---------------------- 
    london     10500                  
    paris      17000            
    Now in one of my books SQL (Mastering Oracle SQL) I found another method by using the SUM, but this time as an analytic function. Here's what the method of the book suggests as an alternative to the problem:
    SELECT city, 
           SUM(SUM(total_sales)) OVER (PARTITION BY city) AS TOTAL_SALES_PER_CITY
    FROM myenterprise
    GROUP BY city
    ORDER BY city, TOTAL_SALES_PER_CITY;
    I know that the analytic functions are executed after the GROUP BY clause has been transformed completely and Unlike regular aggregate functions, they return their result for each line belonging to the partitions specified in the partition clause (if there is a defined partition clause).

    Now my problem is that I do not understand what we have to use two functions SUM? If we only use one only, i.e.
    SELECT city, 
           SUM(total_sales) OVER (PARTITION BY city) AS TOTAL_SALES_PER_CITY
    FROM myenterprise
    GROUP BY city
    ORDER BY city, TOTAL_SALES_PER_CITY;
    This generates the following error:
    Error starting at line 2 in command:
    SELECT city, 
           SUM(total_sales) OVER (PARTITION BY city) AS TOTAL_SALES_PER_CITY
    FROM myenterprise
    GROUP BY city
    ORDER BY city, TOTAL_SALES_PER_CITY
    Error at Command Line:2 Column:11
    Error report:
    SQL Error: ORA-00979: not a GROUP BY expression
    00979. 00000 -  "not a GROUP BY expression"
    *Cause:    
    *Action:
    The error is generated for the line 2 column 11 which is, for the expression SUM (total_sales), well it's true that total_sales does not appear in the GROUP BY clause, but this should not be a problem, it has been used in an analytical function, so it is evaluated after the GROUP BY clause.

    So here's my question:

    Why use SUM (SUM (total_sales)) instead of SUM (total_sales)?


    Thanks in advance!
    :)





    In case you are interested, that's my definition of the table:
    DROP TABLE myenterprise;
    CREATE TABLE myenterprise(
    city VARCHAR2(10), 
    storeid VARCHAR2(10),
    month_name VARCHAR2(10),
    total_sales NUMBER);
    
    INSERT INTO myenterprise(city, storeid, month_name, total_sales)
      VALUES ('paris', 'id1', 'January', 1000);
    INSERT INTO myenterprise(city, storeid, month_name, total_sales)
      VALUES ('paris', 'id1', 'March', 7000);
    INSERT INTO myenterprise(city, storeid, month_name, total_sales)
      VALUES ('paris', 'id1', 'April', 2000);
    INSERT INTO myenterprise(city, storeid, month_name, total_sales)
      VALUES ('paris', 'id2', 'November', 2000);
    INSERT INTO myenterprise(city, storeid, month_name, total_sales)
      VALUES ('paris', 'id3', 'January', 5000);
    INSERT INTO myenterprise(city, storeid, month_name, total_sales)
      VALUES ('london', 'id4', 'Janaury', 3000);
    INSERT INTO myenterprise(city, storeid, month_name, total_sales)
      VALUES ('london', 'id4', 'August', 6000);
    INSERT INTO myenterprise(city, storeid, month_name, total_sales)
      VALUES ('london', 'id5', 'September', 500);
    INSERT INTO myenterprise(city, storeid, month_name, total_sales)
      VALUES ('london', 'id5', 'November', 1000);
    Edited by: dariyoosh on April 9, 2009 04:51

    It is clear that thet Analytics is reduntant here...
    You can even use AVG or any analytic function...

    SQL> SELECT city,
      2         avg(SUM(total_sales)) OVER (PARTITION BY city) AS TOTAL_SALES_PER_CITY
      3  FROM myenterprise
      4  GROUP BY city
      5  ORDER BY city, TOTAL_SALES_PER_CITY;
    
    CITY       TOTAL_SALES_PER_CITY
    ---------- --------------------
    london                    10500
    paris                     17000
    
  • Analytic Functions

    Hello

    Don't you know that it is the analytical function used to access an earlier date for example table RRODUCT

    Date amount CN code
    25/09/2012100020
    26/09/2013200015
    27/09/201110008
    28/09/2012200012
    29/09/201320002
    30/09/20041000

    4

    and this table contains more than 1000 lines in difeerent years now, I want to get the amount in a given year and the previous year like this 20 + 15 + 12 + 4

    I need analytical control that find the previous year 2012 if my year 2013 or find out if 2010 my 2011 yeaar

    You can use the YEAR-1 right? SHIFT of analytic function can be used to access the previous line. Your condition is to get the value of the previous year and previous row not. If this isn't what you are looking for then can you post output necessary for data provided?

  • FIRST_VALUE() and LAST_VALUE Analytic Functions

    Hi all

    May be that it is a fundamental issue. But I'm having a hard time understanding the difference between FIRST_VALUE() and LAST_VALUE() of analytical functions.

    As much as what I read the FIRST_VALUE function picks up the first record after the partition and order by and he returned after all calculation. And the LAST_VALUE does the opposite. But the result of the second query as I expected, are not (last value of the partition). It would be useful that someone could throw some light on it.
    select empno
         , ename
         , sal
         , first_value(ename) over(order by sal desc)
      from emp;
    
    empno  ename      sal       first_value 
    ------ ---------- --------- ----------- 
    7839   KING       5000.00   KING        
    7902   FORD       3000.00   KING        
    7788   SCOTT      3000.00   KING        
    7566   JONES      2975.00   KING        
    7698   BLAKE      2850.00   KING        
    7782   CLARK      2450.00   KING        
    7499   ALLEN      1600.00   KING        
    7844   TURNER     1500.00   KING        
    7934   MILLER     1300.00   KING        
    7654   MARTIN     1250.00   KING        
    7521   WARD       1250.00   KING        
    7876   ADAMS      1100.00   KING        
    7900   JAMES      950.00    KING        
    7369   SMITH      800.00    KING        
    
    14 Row(s) affected
    select empno
         , ename
         , sal
         , last_value(ename) over(order by sal desc)
      from emp;
    
    empno  ename      sal       last_value 
    ------ ---------- --------- ---------- 
    7839   KING       5000.00   KING       
    7902   FORD       3000.00   SCOTT      
    7788   SCOTT      3000.00   SCOTT      
    7566   JONES      2975.00   JONES      
    7698   BLAKE      2850.00   BLAKE      
    7782   CLARK      2450.00   CLARK      
    7499   ALLEN      1600.00   ALLEN      
    7844   TURNER     1500.00   TURNER     
    7934   MILLER     1300.00   MILLER     
    7521   WARD       1250.00   MARTIN     
    7654   MARTIN     1250.00   MARTIN     
    7876   ADAMS      1100.00   ADAMS      
    7900   JAMES      950.00    JAMES      
    7369   SMITH      800.00    SMITH      
    
    14 Row(s) affected
    Thank you
    Vincent

    Hey, Vincent,.

    When you use an analytic function with an ORDER BY clause, the results will be based on a window, which is a subset of the partition.
    If you do not specify a window (using the keywords LINE or LINES) the window everything will be in order by the ORDER BY clause, until and including the current line, including links.

    For example, in your second query:

    select empno
         , ename
         , sal
         , last_value(ename) over(order by sal desc)
      from emp;
    
    empno  ename      sal       last_value
    ------ ---------- --------- ----------
    7839   KING       5000.00   KING
    7902   FORD       3000.00   SCOTT
    7788   SCOTT      3000.00   SCOTT
    7566   JONES      2975.00   JONES
    7698   BLAKE      2850.00   BLAKE
    7782   CLARK      2450.00   CLARK
    7499   ALLEN      1600.00   ALLEN
    7844   TURNER     1500.00   TURNER
    7934   MILLER     1300.00   MILLER
    7521   WARD       1250.00   MARTIN
    7654   MARTIN     1250.00   MARTIN
    7876   ADAMS      1100.00   ADAMS
    7900   JAMES      950.00    JAMES
    7369   SMITH      800.00    SMITH    
    

    The analytic function

    last_value(ename) over(order by sal desc)
    

    Returns the last ename, not of the entire table, but the window starting with the highest sal (since you say "ORDER BY sal DESC") and including the current line and all the other lines that have the same sal.

    So consider the 1st row, ename = 'KING '. It has the most sal, so that a single line in the window, 'KING' IS THE LAST VALUE WINDOW.

    Now consider the 2nd row, where ename = 'FORD' and sal = 3000. The window includes now everybody with a sal of 3000 and more, which means the 3 rows 'KING', 'FORD' and 'SCOTT '. The last of them (in descending order of sal) is 'SCOTT '. (In fact, there is a tie, you could just as well say that "JONES" is changed, because there is a tie between the two rows where sal = 3000) When this happens, one of the lines will arbitrarily designate the "last" line don't expect not to be always the same line.)

    Because of this, LAST_VALUE is alwmost always used with an explicit windowing clause, beginning with the BEACH or LINES.

    If you want a request as your first request, but it contains the name of the lowest paid employee (that is, he always says 'SMITH' in the last column instead of 'KING'), then use FIRST_VALUE, but reverse the sort order:

    first_value(ename) over(order by sal ASC)
    
  • Analytical function in Oracle

    I have a situation where I partitioned a Recordset. If in one partition on this recordset, the value of a field (field name registered) is '45' I need to order the result of this partition by - "outdate" desc "this provision" desc and order the other partition of desc 'key',' sequence ' desc, desc "outdate."

    If the query looks like to.

    Select row_number() over (partition by the order of the keys in sequence) RowNo, key, seq, status, outdate, receivedate from table1 where...
    order by?


    RowNo status outdate Seq key provision
    1 200 0 24 9/13 / 2009 12/9/2009
    2 200 1 23 9/10 / 2009 9/09/2009
    3 200 2 24 9/09 / 2009 9/08/2009

    1 210 0 24 9/13 / 2009 12/9/2009
    2 210 1 * 45 * 9/09/2009-9/08/2009
    3 210 2 24 9/10 / 2009 9/09/2009

    So I need to get the query that will order the first series of partition by desc 'key',' order ' desc, desc "outdate" and the second set of partition (because the status of '45' exists in the second partition) by "outdate" desc "this provision" desc.

    The output of the query should look like

    RowNo status outdate Seq key provision
    1 200 0 24 9/13 / 2009 12/9/2009
    2 200 1 23 9/10 / 2009 9/09/2009
    3 200 2 24 9/09 / 2009 9/08/2009

    1 210 0 24 9/13 / 2009 12/9/2009
    2 210 2 24 9/10 / 2009 9/09/2009
    3 210 1 * 45 * 9/09/2009-9/08/2009

    I don't know if this is possible using the analytical function.

    I would appreciate if any can help me with that.

    Thanks in advance

    Hello

    Welcome to the forum!

    You can use analytical functions in the ORDER BY clause.

    I do not have your tables, so I'll use scott.emp to illustrate.

    The following query sorts first by deptno. After this, the sort order for the departments that contain at least one seller is:
    b job
    (b) ename
    DEPTNO = 30 is be the only Department with a seller, so it's the only sorting as shown above.
    Other departments will be sorted by
    (a) sal
    (b) job

    SELECT       deptno
    ,       ename
    ,       job
    ,       sal
    FROM       scott.emp
    ORDER BY  deptno
    ,            CASE
              WHEN  COUNT ( CASE
                                   WHEN  job = 'SALESMAN'
                          THEN  1
                         END
                       ) OVER (PARTITION BY deptno) > 0
              THEN  ROW_NUMBER () OVER ( PARTITION BY  deptno
                                          ORDER BY        job
                                ,            ename
                              )
              ELSE  ROW_NUMBER () OVER ( PARTITION BY  deptno
                                          ORDER BY        sal
                                ,            job
                              )
           END
    ;
    

    Output:

    .   DEPTNO ENAME      JOB              SAL
    ---------- ---------- --------- ----------
            10 MILLER     CLERK           1300
            10 CLARK      MANAGER         2450
            10 KING       PRESIDENT       5000
            20 SMITH      CLERK            800
            20 ADAMS      CLERK           1100
            20 JONES      MANAGER         2975
            20 SCOTT      ANALYST         3000
            20 FORD       ANALYST         3000
            30 JAMES      CLERK            950
            30 BLAKE      MANAGER         2850
            30 ALLEN      SALESMAN        1600
            30 MARTIN     SALESMAN        1250
            30 TURNER     SALESMAN        1500
            30 WARD       SALESMAN        1250
    

    The small set of sample data you posted, the results you want can be achieved simply through

    ORDER BY  key
    ,         outdate     DESC
    

    I guess it's just a coincidence.

    If you need help, post some examples of data that requires really looking at the status column to get good results. Display the data in executable form, such as CREATE TABLE and the instructions INSERT, olr, as Salim, a WITH clause. (Maybe you can simply add or change a couple of lines in the example Salim already posted data).

  • Deployment of sbRIO 9606 chassis LabVIEW (sbRIO-9606): (Hex 0x80DF001E) could not find the plug-in for this deployment item. Make sure that the appropriate software is installed.

    Hi guys,.

    I have problems getting my sbRIO 9606 to deploy.

    Everything's fine until during the deployment process, I get this:

    Deployment of sbRIO 9606 chassis (sbRIO-9606)
    LabVIEW: (Hex 0x80DF001E) could not find the plug-in for this deployment item. Make sure that the appropriate software is installed.

    I tried to reinstall LV (ensuring that FPGA & RT deployment options are checked) and the 9606 software shipped, but it made no difference. I'm still waiting my next serial key by NOR, I have to wait that I recorded it before I can deploy?

    Please find attached the screenshots of my:

    • License Manager OR
    • Overview of the 9606 MAX
    • My project folder
    • Dialog box deployment where the error is displayed.

    -Adam

    Hi Tim,.

    In time real 11 is installed according to the recommended software battery installation program included on the disc NEITHER RIO provided in the starter pack.

    The problem is that the recommended Software NI Scan Engine 3.0 installation program.

    The sbRIO 9606 does not support scanning engine.

    Once the scanning engine support has been uninstalled, I could deploy on my 9606.

  • Need help with the analytical function select maximum and minimum of the results of the column

    Hey there OTN.

    I have an interesting application that I was hoping you would be able to help me with. I have a requirement to conditionally select the max and min of a column in bi-editor and since my editor works from an OBIEE analysis, I need store MAX and MIN of the column values in separate columns to match with. See the example below. You will notice that there are 4 stores including today's sales. I must have OBIEE through all the results of the column for sales, then choose the max of the dataset object. I can't use MAX here because he will choose the MAX of the line which will return only sales of this line. Instead, one must analyze all sales results and choose the appropriate column. Any idea on how to do this in OBIEE/publisher? Or is this not possible.

    Day Store Sales Sales of MAX Sales MIN
    05/11/15Store 1500080001000
    05/11/15Store 2750080001000
    05/11/15Store 3100080001000
    05/11/15Store 4800080001000

    I'm waiting for your answers. Thanks in advance!

    PS: I will always mark messages that are useful and eventually mark it as correct answer if we come to a resolution!

    See you soon.

    You can't do the same thing with RANK ("dirty")?

    Rank ("dirty") = 1: the max value in the result of sales

    RANK (-1 * "Sales") = 1: the min in the result of sales value

    I guess you can and then format the cells based on these values, where a value of 1 is the max or min according to the RANKING formula you used...

  • SQL using the analytic function


    Hi all

    I want a help in the creation of my SQL query to retrieve the data described below:

    I have a test of sample table containing data as below:

    State ID Desc

    MICHAEL 1 T1

    ACTIVE 2 T2

    T3 3 SUCCESS

    DISABLE THE T4 4

    The thing I want to do is to select all the lines with an ACTIVE status in the table but is there is no ACTIVE status, my request will give me the last line with MICHAEL status.

    I can do this in a single request by using the analytical function for example, if yes can yiu help me on the request of unpacking.

    Kind regards

    Raluce

    Something like that?

    I had to fix it.

    with testdata until)
    Select 1 id, "T1" dsc "DISABLED" status of Union double all the
    Select 2 id, 'T2' dsc, the status "ACTIVE" of all the double union
    Select id 3, "T3" dsc, the status of 'SUCCESS' of all the double union
    Select 4 id, "T4" dsc "DISABLED" status of double
    )

    Select
    ID
    dsc
    status
    of testdata
    where
    status =
    -case when (select count (*) in testdata where status = 'ACTIVE') > 0
    then 'ACTIVE '.
    Another 'DISABLED '.
    end
    and)
    ID in (select id from testdata where status = ' ACTIVE')
    or
    ID = (select max (id) in testdata when status = 'DISABLED')
    )

    STATE ID DSC

    '2' 'T2' 'ACTIVE '.

    Maybe it's more efficient

    Select
    ID
    dsc
    status
    of testdata
    where
    status =
    -case when (select count (*) in testdata where status = 'ACTIVE') > 0
    then 'ACTIVE '.
    Another 'DISABLED '.
    end
    and
    ID =)
    -case when (select count (*) in testdata where status = 'ACTIVE') > 0
    then id
    on the other
    (select max (id) in testdata when status = 'DISABLED')
    end
    )

    Post edited by: correction of chris227

    Post edited by: chris227
    extended

  • Helps the analytic function

    Here is an example of the table data:
    ID    NAME             Start                  
    1     SARA             01-JAN-2006     
    2     SARA             03-FEB-2006     
    3     LAMBDA             21-MAR-2006     
    4     SARA             13-APR-2006     
    5     LAMBDA             01-JAN-2007     
    6     LAMBDA             01-SEP-2007     
    I would get this:
    Name        Start               Stop
    SARA        01-JAN-2006    20-MAR-2006
    LAMBDA      21-MAR-2006     12-APR-2006
    SARA        13-APR-2006     31-DEC-2006
    LAMBDA      01-JAN-2007      <null>
    I tried using partition and run the function but partition name combines all the lines of Sara and Lambda lines into a single group/partition that is not I am trying to get.
    Is there an analytic function or other means to achieve to combine date ranges only when the same person appeared conescutively?
    Thank you.

    This can be easily achieved using tabibitosan:

    First of all, you need to identify 'groups', that each name in the list belongs

    with sample_data as (select 1 id, 'SARA' name, to_date('01/01/2006', 'dd/mm/yyyy') start_date from dual union all
                         select 2 id, 'SARA' name, to_date('03/02/2006', 'dd/mm/yyyy') start_date from dual union all
                         select 3 id, 'LAMBDA' name, to_date('21/03/2006', 'dd/mm/yyyy') start_date from dual union all
                         select 4 id, 'SARA' name, to_date('13/04/2006', 'dd/mm/yyyy') start_date from dual union all
                         select 5 id, 'LAMBDA' name, to_date('01/01/2007', 'dd/mm/yyyy') start_date from dual union all
                         select 6 id, 'LAMBDA' name, to_date('01/09/2007', 'dd/mm/yyyy') start_date from dual)
    select id,
           name,
           start_date,
           lead(start_date, 1, to_date('31/12/9999', 'dd/mm/yyyy')) over (order by start_date) next_start_date,
           row_number() over (order by start_date)
             - row_number() over (partition by name order by start_date) grp
    from   sample_data;
    
            ID NAME   START_DATE NEXT_START_DATE        GRP
    ---------- ------ ---------- --------------- ----------
             1 SARA   01/01/2006 03/02/2006               0
             2 SARA   03/02/2006 21/03/2006               0
             3 LAMBDA 21/03/2006 13/04/2006               2
             4 SARA   13/04/2006 01/01/2007               1
             5 LAMBDA 01/01/2007 01/09/2007               3
             6 LAMBDA 01/09/2007 31/12/9999               3
    

    You can see the group number is generated by comparing the rownumber overall of all lines (in order) with the rownumber of the rowset by name (in the same order) - when there is a gap because another name appears between the two, the group number changes.

    Once you have identified the number of group for each set of rows, it is easy to find the min / max values in this group:

    
    with sample_data as (select 1 id, 'SARA' name, to_date('01/01/2006', 'dd/mm/yyyy') start_date from dual union all
                         select 2 id, 'SARA' name, to_date('03/02/2006', 'dd/mm/yyyy') start_date from dual union all
                         select 3 id, 'LAMBDA' name, to_date('21/03/2006', 'dd/mm/yyyy') start_date from dual union all
                         select 4 id, 'SARA' name, to_date('13/04/2006', 'dd/mm/yyyy') start_date from dual union all
                         select 5 id, 'LAMBDA' name, to_date('01/01/2007', 'dd/mm/yyyy') start_date from dual union all
                         select 6 id, 'LAMBDA' name, to_date('01/09/2007', 'dd/mm/yyyy') start_date from dual),
         tabibitosan as (select id,
                                name,
                                start_date,
                                lead(start_date, 1, to_date('31/12/9999', 'dd/mm/yyyy')) over (order by start_date) next_start_date,
                                row_number() over (order by start_date)
                                  - row_number() over (partition by name order by start_date) grp
                         from   sample_data)
    select name,
           min(start_date) start_date,
           max(next_start_date) stop_date
    from   tabibitosan
    group by name, grp
    order by start_date;
    
    NAME   START_DATE STOP_DATE
    ------ ---------- ----------
    SARA   01/01/2006 21/03/2006
    LAMBDA 21/03/2006 13/04/2006
    SARA   13/04/2006 01/01/2007
    LAMBDA 01/01/2007 31/12/9999
    

    If you want the date to appear as null max, you will need to use a cast or decode to change it - I'll leave that as an exercise for you to do! I'll also let you to find how to get the day before for the stop_date.

  • confusion with the analytical functions

    I created an example where I am right now with the help of analytical functions. However, I need the query below to return an additional column. I need to return the result from:-' factor_day_sales * max (sdus)'. Any ideas?

    If the first column is located and must have the following results

    777777, 5791, 10, 1.5, 15, 90, 135, 7050

    the 1350 is the result, I don't know how to do. (some how to multiply factored_day_sales max (sdus) 15 470 = 7050
    create table david_sales (
    pro_id number(38),
    salesidx number (38,6),
    tim_id number(38));
    
    truncate table david_sales
    
    create table david_compensations (
    pro_id number(38),
    tim_id number(38),
    factor number(38,6));
    
    
    insert into david_sales values
    (777777, 10.00, 5795);
    insert into david_sales values
    (777777,20.00, 5795);
    insert into david_sales values
    (777777, 30.00, 5794);
    insert into david_sales values
    (777777, 40.00, 5794);
    insert into david_sales values
    (777777, 100.00, 5793);
    insert into david_sales values
    (777777, 10.00, 5793);
    insert into david_sales values
    (777777,80.00, 5791);
    insert into david_sales values
    (777777, 10.00, 5791);
    
    insert into david_compensations values
    (777777, 5795, 1.5);
    insert into david_compensations values
    (777777, 5793, 2.0);
    insert into david_compensations values
    (777777, 5792, 1.0);
    insert into david_compensations values
    (777777, 5791, 1.5);
    
    
    
        SELECT  s.pro_id sales_pro
        ,       c.pro_id comp_pro
        ,       s.tim_id sales_tim
        ,       c.tim_id comp_tim
        ,       s.salesidx day_sales
        ,       NVL(c.factor, 1) factor
        ,       s.salesidx * NVL(c.factor, 1) factored_day_sales
        ,       sum(s.salesidx                   ) over (partition by s.pro_id order by s.pro_id, s.tim_id) Sdus
        ,       sum(s.salesidx * NVL(c.factor, 1)) over (partition by s.pro_id order by s.pro_id, s.tim_id) sumMjCj 
          FROM david_sales s
          ,    david_compensations c
          WHERE s.pro_id    = c.pro_id(+)
          AND s.tim_id      = c.tim_id(+)
          AND s.tim_id     BETWEEN 5791  AND 5795
    Thanks for looking

    Is that what you want?

        SELECT  s.pro_id sales_pro
        ,       c.pro_id comp_pro
        ,       s.tim_id sales_tim
        ,       c.tim_id comp_tim
        ,       s.salesidx day_sales
        ,       NVL(c.factor, 1) factor
        ,       s.salesidx * NVL(c.factor, 1) factored_day_sales
        ,       sum(s.salesidx                   ) over (partition by s.pro_id order by s.pro_id, s.tim_id) Sdus
        ,       sum(s.salesidx * NVL(c.factor, 1)) over (partition by s.pro_id order by s.pro_id, s.tim_id) sumMjCj
        , (s.salesidx * NVL(c.factor, 1) * sum(s.salesidx                   ) over (partition by s.pro_id order by s.pro_id, s.tim_id))
          FROM david_sales s
          ,    david_compensations c
          WHERE s.pro_id    = c.pro_id(+)
          AND s.tim_id      = c.tim_id(+)
          AND s.tim_id     BETWEEN 5791  AND 5795
    
    SALES_PRO              COMP_PRO               SALES_TIM              COMP_TIM               DAY_SALES              FACTOR                 FACTORED_DAY_SALES     SDUS                   SUMMJCJ                SUMMEDMULTI
    ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ----------------------
    777777                 777777                 5791                   5791                   80                     1.5                    120                    90                     135                    10800
    777777                 777777                 5791                   5791                   10                     1.5                    15                     90                     135                    1350  
    

    I get the 1350

    or did you mean:

        SELECT  s.pro_id sales_pro
        ,       c.pro_id comp_pro
        ,       s.tim_id sales_tim
        ,       c.tim_id comp_tim
        ,       s.salesidx day_sales
        ,       NVL(c.factor, 1) factor
        ,       s.salesidx * NVL(c.factor, 1) factored_day_sales
        ,       sum(s.salesidx                   ) over (partition by s.pro_id order by s.pro_id, s.tim_id) Sdus
        ,       sum(s.salesidx * NVL(c.factor, 1)) over (partition by s.pro_id order by s.pro_id, s.tim_id) sumMjCj
        ,  s.salesidx * NVL(c.factor, 1) * (sum(s.salesidx * NVL(c.factor, 1)) over (partition by s.pro_id order by s.pro_id, s.tim_id)) summedMulti
          FROM david_sales s
          ,    david_compensations c
          WHERE s.pro_id    = c.pro_id(+)
          AND s.tim_id      = c.tim_id(+)
          AND s.tim_id     BETWEEN 5791  AND 5795 
    
    SALES_PRO              COMP_PRO               SALES_TIM              COMP_TIM               DAY_SALES              FACTOR                 FACTORED_DAY_SALES     SDUS                   SUMMJCJ                SUMMEDMULTI
    777777                 777777                 5795                   5795                   10                     1.5                    15                     300                    470                    7050
    

    Note, in the second block, I changed it just to use sumMjCj instead of sDus which seems to correlate with what you wanted (15 * 470 = 7050) while sdus is 15 * 300 = 4500

    Published by: tanging on December 11, 2009 06:17

  • Using the analytical function.

    Hello

    I have this scenario.
    with t as 
    (
    select 21009 item_id,9 primary_available,450 max_qty,100 bulk_available,12122 bulk_locator_id 
    from dual union all
    select 21009 item_id,9 primary_available,450 max_qty,2775 bulk_available,8704 bulk_locator_id 
    from dual union all
    select 21009 item_id,9 primary_available,450 max_qty,524 bulk_available,15614 bulk_locator_id 
    from dual union all
    select 21009 item_id,9 primary_available,450 max_qty,3300 bulk_available,15654 bulk_locator_id
    from dual)
    select  t.* from t;
    We have two locations for a given item_id. Primary and in bulk.

    I'm trying to get a select statement out of this point of view, where I will be restock the primary AMOUNT of sites in bulk, BUT the smaller bulk first. Once she gets up, I shouldn't take more product.

    There is an analytic function that would do this?

    That's the max I could come up with.
    with t as 
    (
    select 21009 item_id,9 primary_available,450 max_qty,100 bulk_available,12122 bulk_locator_id 
    from dual union all
    select 21009 item_id,9 primary_available,450 max_qty,2775 bulk_available,8704 bulk_locator_id 
    from dual union all
    select 21009 item_id,9 primary_available,450 max_qty,524 bulk_available,15614 bulk_locator_id 
    from dual union all
    select 21009 item_id,9 primary_available,450 max_qty,3300 bulk_available,15654 bulk_locator_id
    from dual)
    select  t.*, max_qty -
                   (primary_available + SUM(bulk_available)
                    over(PARTITION BY item_id ORDER BY bulk_available)) replen_this_much 
                    from t;
    So, in this scenario, I want to replen bulk_locator_id 100 ' 12122 'and ' 15614 bulk_locator_id 341'. That's all. ZERO of the other rentals (bulk_locator_id). If the question is not clear, please let me know.

    Published by: RPuttagunta on September 11, 2009 16:23

    Hello

    Thanks for posting the sample data.
    It would be useful that you also posted the output you want. Is this?

    .                                 BULK_         REPLEN_
    ITEM_ PRIMARY_   MAX_  BULK_      LOCATOR_        THIS_
    ID    AVAILABLE  QTY   AVAILABLE  ID               MUCH
    ----- ---------- ----- ---------- ---------- ----------
    21009 9          450   100        12122             100
    21009 9          450   524        15614             341
    21009 9          450   2775       8704                0
    21009 9          450   3300       15654               0
    

    If so, you can get to this:

    SELECT       t.*
    ,       GREATEST ( 0
                 , LEAST ( TO_NUMBER (bulk_available)
                         , TO_NUMBER (max_qty)
                        - ( TO_NUMBER (primary_available)
                          + NVL ( SUM (TO_NUMBER (bulk_available))
                                  OVER ( PARTITION BY  item_id
                                         ORDER BY      TO_NUMBER (bulk_available)
                                   ROWS BETWEEN  UNBOUNDED PRECEDING
                                     AND      1           PRECEDING
                                 )
                             , 0
                             )
                          )
                      )
                 ) AS replen_this_much
    FROM       t
    ORDER BY  item_id
    ,            TO_NUMBER (bulk_available)
    ;
    

    You should really store your numbers in NUMBER of columns.

    You essentially posted all what you need analytical functions. The problem was just wrapping this analytical function (or something very close to it) and LESS and more GRAND, so that the replen_this_much column is always between 0 and TO_NUMBER (bulk_available).

  • Download a update, I get this message,"you don't have the appropriate permissions. Choose another directory for registration. How to choose another directory for registration?

    Trying to update from Mozilla, I get the error message following, "the file would be not saved because you do not have the appropriate permissions. Choose another directory for registration.

    How to choose another "directory" of record?

    Press Alt + E and then N to bring up your preferences. On the general tab, check the option "always ask me where to save files.

Maybe you are looking for

  • Satellite C660-15R: no applications will organize

    Hello, hoping you can help me. When I start my laptop, it starts as usual, connects to networks etc but when I try to run firefox, microsoft Word, or any other application, it won't.I discovered that I can restart in safe mode, and then restart the l

  • y500p, remote control issues

    the title suggests, my lenovo y500p VOLUME remote control does not work, specifically the volume and the mute button, the play, the buttons fast forward etc. etc. also does not work with wmp or wmc. The problem is that he actually used to work. Pleas

  • Google Wallet payment fails on OneNote upgrade

    My Google Wallet payment continues to fail when I try to upgrade my OneNote.  The charges will thrues to my credit card company, but Microsoft refuses... This is the message I get... Your order is complete. Do you have questions? Contact Microsoft Co

  • Updates are ready for Adobe Reader, but get error: invalid when Player tries to download

    Get notified that updates are ready to download from Adobe, but when I try to download they say error. Invalid drive H:\. I have a H drive in recent years, but it does not give me the ability to place on my C or G discs. How can I get these updates i

  • CD Wrighter does not recognize the CD - R blank CD

    IM tryin to burn music, when I put to burn on a CD - R, it says PLEASE INSERT DISK...     It is not recognizeing the blank CD, Ive tried 3 different CD - Rs, but keep the same message, IM RUNNING WINDOWS XP WITH MEDIA PLAYER 11?  ?  ?Please HELP, THA