Analytical function - return 2 values

I don't know I need to use an analytical function to do this, I can't do things. I appreciate the help.
Statements of table and insert:
create table TST_CK
(
DOC_ID NUMBER(6)      not null,
ROW_SEQ_NBR NUMBER(6) not null,
IND_VALUE VARCHAR2(2) null
)
INSERT INTO TST_CK VALUES ('1','6',NULL);
INSERT INTO TST_CK VALUES ('1','5',NULL);
INSERT INTO TST_CK VALUES ('1','4','T');
INSERT INTO TST_CK VALUES ('1','3','R');
INSERT INTO TST_CK VALUES ('1','9',NULL);
INSERT INTO TST_CK VALUES ('1','10',NULL);
INSERT INTO TST_CK VALUES ('1','7','T');
INSERT INTO TST_CK VALUES ('1','8','R');
INSERT INTO TST_CK VALUES ('2','1',NULL);
INSERT INTO TST_CK VALUES ('2','2',NULL);
INSERT INTO TST_CK VALUES ('2','3','T');
INSERT INTO TST_CK VALUES ('2','4','R');
INSERT INTO TST_CK VALUES ('2','5',NULL);
INSERT INTO TST_CK VALUES ('2','6',NULL);
INSERT INTO TST_CK VALUES ('2','7','T');
INSERT INTO TST_CK VALUES ('2','8','R');
INSERT INTO TST_CK VALUES ('4','1',NULL);
INSERT INTO TST_CK VALUES ('4','2',NULL);
INSERT INTO TST_CK VALUES ('4','3','X1');
INSERT INTO TST_CK VALUES ('4','4',NULL);
INSERT INTO TST_CK VALUES ('4','5',NULL);
INSERT INTO TST_CK VALUES ('4','6',NULL);
INSERT INTO TST_CK VALUES ('4','7','T');
INSERT INTO TST_CK VALUES ('4','8','R');
INSERT INTO TST_CK VALUES ('4','9',NULL);
INSERT INTO TST_CK VALUES ('4','10',NULL);
INSERT INTO TST_CK VALUES ('4','11',NULL);
INSERT INTO TST_CK VALUES ('4','12',NULL);
INSERT INTO TST_CK VALUES ('4','13','T');
INSERT INTO TST_CK VALUES ('4','14','R');
INSERT INTO TST_CK VALUES ('4','15',NULL);
INSERT INTO TST_CK VALUES ('4','16',NULL);
COMMIT;
Here is what I tried that gets me close:
SELECT MAX (TST_CK.DOC_ID), MAX (TST_CK.ROW_SEQ_NBR), TST_CK.IND_VALUE
  FROM ASAP.TST_CK TST_CK
 WHERE (TST_CK.IND_VALUE IS NOT NULL)
GROUP BY TST_CK.IND_VALUE
ORDER BY 2 ASC
Here is my desired outcome:
CV_1      CV_2
T           R
Or even better result would be:
concat(CV_1,CV_2)
Result:
T,R
Thanks for looking

G

GMoney says:
Basically, I just need to make sure what I captures the last T and R entered in the column

Still not clear. You want to find the last two lines (function ROW_SEQ_NBR) where IND_VALUE is 't' & 'R', right? If so:

with t as (
           select  doc_id,
                   row_seq_nbr,
                   ind_value,
                   row_number() over(partition by ind_value order by row_seq_nbr desc) rn
             from  tst_ck a
             where ind_value in ('T','R')
          )
select  doc_id,
        row_seq_nbr,
        ind_value
  from  t
  where rn = 1
/ 

    DOC_ID ROW_SEQ_NBR IN
---------- ----------- --
         4          14 R
         4          13 T

SQL> 

SY.

Tags: Database

Similar Questions

  • Function return undefined values

    My function returns undefined values. It seems that the return of the function statement is executed before the database is queried and values are assigned to variables. If I do an alert within the loop for () I get the correct values from the database, but these values are not affected before excecuting the return statement function. How can I do this differently?

    function getColumnNames(table){
        var rs1, rs2, rs3, rs4, rs5;
        db.transaction(function (tx) {
            tx.executeSql("SELECT * FROM schema WHERE table_name=?",[table], function(ax, results){
                var size = results.rows.length, i;
                var item;
                for (i = 0; i < size; i++){
                    item = results.rows.item(i);
                    rs1 = item.col1;
                    rs2 = item.col2;
                    rs3 = item.col3;
                    rs4 = item.col4;
                    rs5 = item.col5;
                                    alert(rs1); //correct value is returned here
                }
            });
        });
        return [rs1, rs2, rs3, rs4, rs5];
    }
    

    Hey Fabian,.

    You are quite correct that the return statement is executed before the database returns the results. There are ongoing asynchronous calls. You can set breakpoints in JavaScript code to see what is happening. You can correct the problem by using function callbacks. I edited your code below to use the callback functions - there might be a few typos, but the general approach should be wooded.

    function getColumnNames(table, callback){
        var rs1, rs2, rs3, rs4, rs5;
        db.transaction(function (tx) {
            tx.executeSql("SELECT * FROM schema WHERE table_name=?",[table], function(ax, results){
                var size = results.rows.length, i;
                var item;
                for (i = 0; i < size; i++){
                    item = results.rows.item(i);
                    rs1 = item.col1;
                    rs2 = item.col2;
                    rs3 = item.col3;
                    rs4 = item.col4;
                    rs5 = item.col5;
                        alert(rs5); //correct values are being returned
                    callback([rs1, rs2, rs3, rs4, rs5]);
                }
            });
        });
    }
    
    // example of how to call getColumnNames with callback
    getColumnNames('importantTable', function (result)
    {
        console.log(result);
    });
    
    // alternative version
    function onCallback(result)
    {
        console.log(result);
    }
    
    getColumnNames('importantTable', onCallback);
    

    I hope this helps.

    See you soon,.

    James

  • ORA-06503: PL/SQL: function returned no value ORA-06512:

    Hi all

    SQL > set serveroutput on

    SQL > CREATE OR replace FUNCTION qty_value (p_item_id number)

    2 RETURN NUMBER

    3 EAST

    4 v_qty_arrival NUMBER;

    5 BEGIN

    6. SELECT THE CASE SENSITIVE OPTION

    7. WHAT (SUM (b.quantity) - SUM (b.quantity_received)) < = 0 THEN 0

    8 ELSE (SUM (b.quantity) - SUM (b.quantity_received))

    9 END Qty_Arrival

    10 INTO v_qty_arrival

    Po_lines_all 11 a,

    po_line_locations_all 12 b

    13 WHERE a.po_line_id = b.po_line_id

    14 AND a.item_id = p_item_id;

    15 EXCEPTION

    16 THEN THAN OTHERS THEN

    17 v_qty_arrival: = NULL;

    18 RETURN v_qty_arrival;

    19 END qty_value;

    20.

    The function is created.

    SQL >

    SQL > select xxc_qty_arrivale (214960) double

    2.

    Select xxc_qty_arrivale (214960) double

    *

    ERROR on line 1:

    ORA-06503: PL/SQL: function returned no value

    ORA-06512: at the 'APPS '. XXC_QTY_ARRIVALE', line 19

    Back AFTER using the exception block is fine as long as your code actually REACHED the exception block.

    When there are no errors, then your code will not enter the exception block, but you still NEED to return a value, since it is what functions a function wants to return a value, that's what the functions are made for.

    Spot the differences between the following 3 functions. Understand how they work.

    The first function has only a return in the exception block but runs correctly. Result: error, because the back is missing in the code block.

    The second function has a return in the exception block and one in the block of code and runs correctly. Result: no error, because the return is not missing in the code block.

    The third function has a return in the exception block and one in the block of code and is forced into an error. Result: no error, because the return of the exception handler is used (instead of retriggering of the error, which you would normally do).

    SQL > create or replace function myfunc

    2 return number

    3 as

    4 start

    5 dbms_output.put_line ('in the section of code');

    6 null;

    7 exception

    8 then than others

    9. can

    10 dbms_output.put_line (' in the exception handler ' |) SQLERRM);

    11      --

    12 return 0;

    13      --

    14 end;

    15.

    The function is created.

    SQL > select double myfunc;

    Select double myfunc

    *

    ERROR on line 1:

    ORA-06503: PL/SQL: function returned no value

    ORA-06512: at "GHPLUS. MYFUNC", line 14

    In the section of code

    SQL > create or replace function myfunc

    2 return number

    3 as

    4 start

    5 dbms_output.put_line ('in the section of code');

    6 null;

    7    --

    8 return 1;

    9    --

    10 exceptional

    11 so that others

    12. can

    13 dbms_output.put_line (' in the exception handler ' |) SQLERRM);

    14      --

    15 return 0;

    16      --

    end 17;

    18.

    The function is created.

    SQL > select double myfunc;

    MYFUNC

    ----------

    1

    1 selected line.

    In the section of code

    SQL > create or replace function myfunc

    2 return number

    3 as

    n number 4;

    5. start

    6 dbms_output.put_line ('in the section of code');

    7 n: = 1/0; -force an error (zero divisor) to join the exception handler

    8    --

    9 return 1;

    10-

    exception 11

    12 so that others

    13. can

    14 dbms_output.put_line (' in the exception handler ' |) SQLERRM);

    15      --

    16 return 0;

    17      --

    18 end;

    19.

    The function is created.

    SQL > select double myfunc;

    MYFUNC

    ----------

    0

    1 selected line.

    In the section of code

    In the handler for exception ORA-01476: divisor is equal to zero

    SQL >

  • ORA-06503: PL/SQL: function returned no value

    Hi guys,.
    I wrote a function that returns the number of quarter for a given date.
    Here's the function

    FUNCTION to CREATE or REPLACE fn_get_quarter
    (p_date DATE)
    RETURN NUMBER
    AS
    v_qtr NUMBER;
    BEGIN
    Select TO_CHAR (p_date, 'Q')
    IN v_qtr
    DOUBLE;
    dbms_output.put_line (' it's quarter ' | v_qtr);
    END;

    It compiled without error.

    Here is the test case that I wrote

    DECLARE
    p_date DATE: = TO_DATE('12/22/2009','MM/DD/YYYY');
    v_qtr NUMBER;
    BEGIN
    v_qtr: = fn_get_quarter (p_date);
    END;

    When I run the present, I get the following error.

    ORA-06503: PL/SQL: function returned no value
    ORA-06512: at "GPS_FVO. FN_GET_QUARTER', line 13
    ORA-06512: at line 7

    But surprisingly, I get the result.

    Please suggest and let me know how to get rid of this error and why I get this error.

    Thanks in advance.
    Sam

    RETURN v_qtr;
    should be added after DBMS_OUTPUT

  • FIRST analytical functions returns the null line

    The following query returns a null line when there is no such thing as a line for the predicate. Is it a bug or expected behaviour. If so what is the meaning of the null row, is it not logical?



    SELECT NVL (max (ah.fyr_end_dt) KEEP (DENSE_RANK FIRST ORDER BY ah.as_of_dt DESC), ' RETURNED NULL')
    Of account_history ah
    WHERE
    Ah.acct_id = 999
    AND trunc (ah.as_of_dt) < = 31 December 08 '
    ;

    Hello

    user4900730 wrote:
    Moreover, in my example how I distinguish the case where a line really exists, but the column is null when there is no such thing as a line and the FIRST function returns the same value null? Hopefullly I should be able to do it in a single SQL and know the difference?

    COUNT (*) > 0, but COUNT (col_x) = 0, then there is a line, but col_x happened to be null.

    For example, the following query shows that there is 1 row in the group with the lowest name ("ADAMS"), but the column comm in all ranks of this group is null:

    SELECT     COUNT (*)    KEEP (DENSE_RANK FIRST ORDER BY ename)
                              AS total_cnt
    ,     COUNT (comm) KEEP (DENSE_RANK FIRST ORDER BY ename)
                              AS comm_cnt
    FROM     scott.emp
    ;
    

    Output:

     TOTAL_CNT   COMM_CNT
    ---------- ----------
             1          0
    

    The employee whose highest name ("WARD") has a number in the comm column.

  • Function - return multiple values in a concatenated string

    Hello

    Is it possible for a function return a concatenated string (combining more than one number). I'm trying to return the purchase by the introduction and invoice_id number and code order number, but an invoice_id can have several PO with the same code number. I want to concatenate the PO numbers with commas, for example

    10124, 10090, 10987

    At the present time, the function returns NULL for those cases that have more than one purchase order number.

    Thank you
    func_get_po(i.invoice_id, cc.code_combination_id) "PO No",
    func_get_po_requester(i.invoice_id, cc.code_combination_id, func_get_po(i.invoice_id, cc.code_combination_id)) "PO Requester",
    

    Of course, this can be done without any problem.
    If it returns NULL, then you must debug function and check the operating logic. I know that it will take a little of your time, but not problems with the logic of syntax and concatenation.

  • APEX 4.0.1: $v () function returns multiple values for the box?

    Hello

    I have a report that uses apex_item.checkbox (...) to generate the check box. This report correctly displays a check box for each line. The source code that is generated in the html page is:
    < input type = "checkbox" name = "f01" value = "202" id = "P1_CHECKBOX" / >
    ...
    ...
    < input type = "checkbox" name = "f01" value = "220" id = "P1_CHECKBOX" / >
    ...
    ...
    < input type = "checkbox" name = "f01" value = "210" id = "P1_CHECKBOX" / >
    ...
    ...

    I want to use the javascript function $v () to get the values of the enabled check box. I thought that the return of this function all the checked values separated by ':' but I noticed that my code alert ($v ('P1_CHECKBOX')); return whenever the value of the first checkbox if it is checked.
    It returns '202' if the first box is checked, but nothing, if only the second checkbox is checked and '202' if the box of the first and the second is checked.

    Hello

    first of all, $v, $x and $s are supposed to not work for items on the page, not the columns in a table or manually generated HTML elements.

    Secondly, I think that your HTML code is not correct, because each of your boxes has the same ID. But the ID must be unique in the DOM of the browser tree. Thus the different box should actually named P1_CHECKBOX_1... P1_CHECKBOX_3. Just look at what is actually generated for a page element real checkbox. BTW, I think that you should not name the checkbox as part of page elements, because they are not actually page elements. I think that this could be confusing for other developers.

    Hope that helps
    Patrick
    -----------
    My Blog: http://www.inside-oracle-apex.com
    APEX 4.0 Plug-Ins: http://apex.oracle.com/plugins
    Twitter: http://www.twitter.com/patrickwolf

  • Function returned no value error

    create or replace function dssppv.hra_find_short_path (
       p_new_rgmn_grp_gid    in   number
      ,p_tail_rgmn_grp_gid   in   number
      ,p_mkt_cnfgr_gid       in   number
    )
       return string
    
    is
       v_rgmn_grp_path varchar2 ( 2000 );
       check_step varchar2 ( 2000 );
    begin
       dbms_output.put_line
          ( 'Begin of function  hra_find_short_path to Check Circular Loop : '
            || to_char ( sysdate, 'YYYY-MM-DD HH24:MI:SS' )
          );
       check_step                 := ' Step 1 - Circular loop Check up.';
    
       --Checking for the circular loop
       select rgmn_path || '/' || gt.rgmn_grp_nam
       into   v_rgmn_grp_path
       from   ( select  min_rgmn_node
                       ,rgmn_path
                       ,rgmn_grp_gid
               from     ( select    level as min_rgmn_node
                                   ,dt.rgmn_grp_gid
                                   ,dt.tail_rgmn_grp_gid
                                   ,gp.rgmn_grp_nam
                                   ,rtrim
                                       ( reverse
                                            ( sys_connect_by_path
                                                      ( reverse ( gp.rgmn_grp_nam )
                                                       ,'/ '
                                                      )
                                            )
                                        ,'/ '
                                       ) as rgmn_path
                         from       dssppv.t_market_cnfgr_rgmn_grp_dtl dt join dssppv.t_market_cnfgr_rgmn_grp gp
                                    on (     dt.rgmn_grp_gid = gp.rgmn_grp_gid
                                         and gp.mkt_cnfgr_gid = p_mkt_cnfgr_gid
                                       )
                         start with dt.tail_rgmn_grp_gid = p_new_rgmn_grp_gid
                         connect by nocycle prior dt.rgmn_grp_gid =
                                                              dt.tail_rgmn_grp_gid )
               where    rgmn_grp_gid = p_tail_rgmn_grp_gid
               order by min_rgmn_node )
             ,dssppv.t_market_cnfgr_rgmn_grp gt
       where  gt.rgmn_grp_gid = p_new_rgmn_grp_gid and rownum = 1;
    
       dbms_output.put_line ( ' Circular Loop is formed as shown :'
                              || v_rgmn_grp_path
                            );
    
       if ( v_rgmn_grp_path is not null )
       then
          dbms_output.put_line ( ' Circular Loop is formed as shown :'
                                 || v_rgmn_grp_path
                               );
       else
          dbms_output.put_line ( ' No Loop is formed.' );
       end if;
    
       dbms_output.put_line
             ( 'End of function  hra_find_short_path to Check Circular Loop : '
               || to_char ( sysdate, 'YYYY-MM-DD HH24:MI:SS' )
             );
       return v_rgmn_grp_path;
    exception
     --  when no_data_found
      -- then
       --   dbms_output.put_line
        --     ( 'End  of function  hra_find_short_path to Check Circular Loop : '
         --      || to_char ( sysdate, 'YYYY-MM-DD HH24:MI:SS' )
          --   );
          --return null;
       when others
       then
          dbms_output.put_line
                         ( 'Error in function DSSPPV.hra_find_short_path at: '
                           || check_step
                         );
          dbms_output.put_line ( sqlerrm );
          dbms_output.put_line
             ( 'End of function  hra_find_rgmn_grp_loops to Check Circular Loop : '
               || to_char ( sysdate, 'YYYY-MM-DD HH24:MI:SS' )
             );
    end hra_find_short_path;
    /
    I have fully commented goal no. Data Exception not found top check if she is to return null when no record is found.

    But he was wrong in not found exception when there is no data record.

    But when run sql separately he showed zero records without don't raise the no found error of data.

    He would not even through the loop fi... Please correct me if I did something wrong
     if ( v_rgmn_grp_path is not null )
       then
          dbms_output.put_line ( ' Circular Loop is formed as shown :'
                                 || v_rgmn_grp_path
                               );
       else
          dbms_output.put_line ( ' No Loop is formed.' );
       end if;

    You may not assume that v_rgmn_grp_path will retain the NULL value if the query returns no rows. In this case AS the value of the variable is not defined. I suggest yo change:

    Select rgmn_path. '/' || gt.rgmn_grp_nam

    TO

    Select count (*) case when 1 then max(rgmn_path ||) '/' || end of gt.rgmn_grp_nam)

    This way NO_DATA_FOUND will not be raised and the v_rgmn_grp_path will be NULL if there is no corresponding line.

    SY.

  • Calendar function returns strange values

    Can someone please help

    Calendar rightNow = Calendar.GetInstance ();
    int aa = Calendar.DATE;
    int bb = Calendar.MONTH;

    in aa value is 5, it is and in the bb, it is 2.

    I checked my date system as well as the date of Simulator. I don't know why it's happening.

    These values Calendar.DATE and Calendar.MONTH are the contacts to be used in conjueciton with the Calendar.getField () method.

    Like this:

    int = rightNow.getField (Calendar.MONTH) MB

  • MONTHS_BETWEEN function returns negative values 'bad '?

    Hello

    I can't understand why these values are results.

    SQL > select months_between (to_date (February 2, 13 ',' DD-MON-RR'), to_date ('February 28, 13 ', ' DD-MON-RR')) twice;

    MONTHS_BETWEEN(TO_DATE('02-FEB-13','DD-MON-RR'),TO_DATE('28-FEB-13','DD-MON-RR')

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

    -.83870968

    SQL > select months_between (to_date (February 2, 13 ',' DD-MON-RR'), to_date ('April 1, 13 ', ' DD-MON-RR')) twice;

    MONTHS_BETWEEN(TO_DATE('02-FEB-13','DD-MON-RR'),TO_DATE('01-APR-13','DD-MON-RR')

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

    -1.9677419

    SQL > select months_between (to_date (February 2, 13 ',' DD-MON-RR'), to_date ('April 2, 13 ', ' DD-MON-RR')) twice;

    MONTHS_BETWEEN(TO_DATE('02-FEB-13','DD-MON-RR'),TO_DATE('02-APR-13','DD-MON-RR')

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

    -2

    user13393428 wrote:

    But,

    Select months_between (to_date (February 2, 13 ',' DD-MON-RR'), to_date ('April 2, 13 ', ' DD-MON-RR')) twice;

    How it is-2? Should not be-1?

    Do you really think there's only 1 month between February and April?  you have a very convenient calendar you can watch...

  • I need to return multiple values in function

    create or replace function f (p) in varchar2) return varchar2
    is
    a number (10);
    Start

    for loop 1 in 1.10
    Select instr('yyyyyyyyynnnnnyynny','y',1,i) in the doubles.
    end loop;
    return a;
    end;

    my function return a value, but I need to return multiple values
    Thanks in advance

    I don't know if that's what you want (you should give expected results also)

    SQL> create or replace type mytabletype as table of number;
      2  /
    
    Type created.
    
    SQL> create or replace function f (p in varchar2) return myTableType pipelined is
      2    a number(10);
      3  begin
      4    for i in 1..10 loop
      5      select instr('yyyyyyyyynnnnnyynny','y',1,i) into a from dual;
      6      pipe row (a);
      7    end loop;
      8  end;
      9  /
    
    Function created.
    
    SQL> select * from table(f('1'));
    
    COLUMN_VALUE
    ------------
               1
               2
               3
               4
               5
               6
               7
               8
               9
              15
    
    10 rows selected.
    
    SQL>
    

    Published by: Leo Mannhart on March 22, 2011 11:01

    could even be compressed into

    create or replace function f (p in varchar2) return myTableType pipelined is
    begin
      for i in 1..10 loop
        pipe row (instr('yyyyyyyyynnnnnyynny','y',1,i));
      end loop;
    end;
    
  • How to return two values of a function?

    Hi all
    I have a function which returns a varchar2 value. This value is used in the SQL of a ref cursor

    Now I want this function returns two values. I tried to use varray, but could not accomplish the task.

    Can you tell me how can I find the two values from a function? and how this function can be called in a SQL query?

    Thank you very much

    The query should look like:

    SELECT v.col.name AS name
         , v.col.flag AS flag
    FROM   ( SELECT kk_test_kk(t.somecolumn,t.someothercolumn,42) AS col
             FROM   sometable t ) v;
    

    Note that you need to prefix with the name of view inline ('v' in the example) and the expression of the function ('col' in the example).

  • analytical function and the aggregate function

    What are the analytical function and the aggregate function. What is the difference between them?

    Hello

    Analytic Functions : -.

    Analytical functions calculate a value of aggregation based on a group of lines. They differ from aggregate functions because they return several rows for each group. The Group of rows is called a window and is defined by the analytic_clause. For each line, a sliding window of lines is defined. The window determines the range of lines used for the calculations for the current line. Window sizes can be based on a physical number of rows or a logic as the time interval.
    Analytical functions are the last set of operations performed in a query with the exception of the last ORDER BY clause. Every joint and every WHERE, GROUP BY and HAVING clauses are met before the analytical functions are handled. As a result, analytic functions can only appear in the select list or the ORDER BY clause.
    Analytical functions are commonly used to calculate cumulative aggregates, moving, centered and considered.

    Aggregate functions : -.

    Aggregate functions return a line of single result based on the groups of lines, rather than on the unique lines. Aggregate functions can appear in selection lists, as well as in the HAVING and ORDER BY clauses. They are commonly used with the GROUP BY clause in a SELECT statement, where Oracle Database splits the rows in a table when asked or seen in groups. In a query that contains a GROUP BY clause, the select list items can be aggregation functions, GROUP BY constant expressions or expressions involving one of them. Oracle applies the functions of aggregation for each group of rows and returns a single result for each group line.
    If you omit the GROUP BY clause, Oracle then applies any aggregate functions in the select list for all rows in the table queried or the view. You use aggregate functions in the HAVING clause to eliminate groups of the output based on the results of aggregate functions, rather than the values of the individual lines of the queried table or view.

    Let me know if you feel any problem understanding.
    Thank you.

    Published by: varun4dba on January 27, 2011 15:32

  • Why the different values for an analytic function of the same group/game

    I have the suite of table I'll be using.

    Select * from table1;

    REC_ID | STATUS | DATE_FROM | DATE_TO

    1. C | 7 January 2015 |

    2. H | December 3, 2014. 6 January 2015

    3. H | October 3, 2014. December 2, 2014

    4. H | May 30, 2014. October 2, 2014

    5. H | May 29, 2014 | May 29, 2014

    6. H | April 16, 2014 | May 28, 2014

    7. H | Tuesday, April 25, 2007 April 15, 2014

    INSERT statement if you need.

    TOGETHER TO DEFINE

    CREATE THE TABLE1 TABLE:

    (

    NUMBER OF REC_ID,

    VARCHAR2 (1 BYTE) STATUS NOT NULL,.

    DATE_FROM DATE NOT NULL,

    DATE OF DATE_TO

    );

    Insert into TABLE1

    (REC_ID, STATUS, DATE_FROM)

    Values

    (1, 'C', TO_DATE (7 JANUARY 2015 00:00:00 ',' DD/MM/YYYY HH24:MI:SS'));))

    Insert into TABLE1

    (REC_ID, STATUS, DATE_FROM, DATE_TO)

    Values

    (2, 'H', TO_DATE (3 DECEMBER 2014 00:00:00 ',' DD/MM/YYYY HH24:MI:SS'), TO_DATE (6 JANUARY 2015 00:00:00 ',' DD/MM/YYYY HH24:MI:SS'));))

    Insert into TABLE1

    (REC_ID, STATUS, DATE_FROM, DATE_TO)

    Values

    (3, 'H', TO_DATE (3 OCTOBER 2014 00:00:00 ',' DD/MM/YYYY HH24:MI:SS'), TO_DATE (2 DECEMBER 2014 00:00:00 ',' DD/MM/YYYY HH24:MI:SS'));))

    Insert into TABLE1

    (REC_ID, STATUS, DATE_FROM, DATE_TO)

    Values

    (4, 'H', TO_DATE (MAY 30, 2014 00:00:00 ',' DD/MM/YYYY HH24:MI:SS'), TO_DATE (2 OCTOBER 2014 00:00:00 ',' DD/MM/YYYY HH24:MI:SS'));))

    Insert into TABLE1

    (REC_ID, STATUS, DATE_FROM, DATE_TO)

    Values

    (5, 'H', TO_DATE (29 MAY 2014 00:00:00 ',' DD/MM/YYYY HH24:MI:SS'), TO_DATE (29 MAY 2014 00:00:00 ',' DD/MM/YYYY HH24:MI:SS'));))

    Insert into TABLE1

    (REC_ID, STATUS, DATE_FROM, DATE_TO)

    Values

    (6, 'H', TO_DATE (APRIL 16, 2014 00:00:00 ',' DD/MM/YYYY HH24:MI:SS'), TO_DATE (28 MAY 2014 00:00:00 ',' DD/MM/YYYY HH24:MI:SS'));))

    Insert into TABLE1

    (REC_ID, STATUS, DATE_FROM, DATE_TO)

    Values

    (7, 'H', TO_DATE (APRIL 25, 2007 00:00:00 ',' DD/MM/YYYY HH24:MI:SS'), TO_DATE (APRIL 15, 2014 00:00:00 ',' DD/MM/YYYY HH24:MI:SS'));))

    COMMIT;

    I will exercise more analytical query...

    Select rec_id date_from, date_to, status,

    min (date_from) over (partition by order of status by date_from desc) min_dt_from_grp,

    ROW_NUMBER() over (partition by order of status by date_from desc) rownumberdesc,

    ROW_NUMBER() over (partition by order of status by ASC date_from) rownumberasc

    FROM table1;

    the query result

    REC_ID | DATE_FROM | DATE_TO | STATUS | MIN_DT_FROM_GRP | ROWNUMBERDESC | ROWNUMBERASC

    1. 7 January 2015 | C | 7 January 2015 | 1. 1

    2. December 3, 2014. 6 January 2015 | H | December 3, 2014. 1. 6

    3. October 3, 2014. December 2, 2014 | H | October 3, 2014. 2. 5

    4. May 30, 2014. October 2, 2014 | H | May 30, 2014. 3. 4

    5. May 29, 2014 | May 29, 2014 | H | May 29, 2014 | 4. 3

    6. April 16, 2014 | May 28, 2014. H | April 16, 2014 | 5. 2

    7. Tuesday, April 25, 2007 April 15, 2014. H | Tuesday, April 25, 2007 6. 1

    If you look at the output above, it dates back in the min_dt_from_grp column.

    MY question is if the analytical function calculates for a particular/set group, which is by statute and for what min (date_from) partition is 25-apr-2007 for the GROUP H (Status column), then why I have different values returned by the query above in the min_dt_from_grp column.

    Hello

    Because you have specified an ORDER BY clause for the analytical function. In doing so, you calculate the rows on a window. Since you have not specified a windowing clause, the default applies:

    RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW

  • Return multiple values from a function in a SELECT statement

    I hope I've provided enough information here. If not, let me know what I'm missing.

    I create a view that will combine the information from several tables. Most are pretty simple, but there are a couple of columns in the view that I need to get by running a function within a package. Even if this is quite simple (I have a function named action_date in a package called rp, for example, that I can use to return the date that I need through SOME rp.action_date (sequence_number).

    Here is the question: I really need to return several bits of information of the same record (not only action_date, but also action_office, action_value, etc.)-a join of the tables will work not here, as I will explain below. I can, of course, perform a function separate for each statement, but this is obviously inefficient. Within the select statement of the view, however, I don't know how each of the values that I need to get back.

    For example, right now, I have:

    Table 1:
    sequence_number NUMBER (10),
    name varchar (30),
    ...

    Table2:
    Table1_seq NUMBER (10),
    action_seq NUMBER (10),
    action_date DATE,
    action_office VARCHAR (3),
    action_value VARCHAR (60),
    ...

    I can't just simply join Table1 and Table2 because I have to perform processing in order to determine the rows returned matching, I really need to select. If the package opens a cursor and treats each line until it finds the one I need.

    The following works but is ineffective since all calls to the package returns the columns of the same record. I don't know how to put all the values that I need in the SELECT statement.
    CREATE VIEW all_this_stuff AS
    SELECT sequence_number, name,
    RP.action_date (sequence_number) action_date,
    RP.action_office (sequence_number) action_office,
    RP.action_value (sequence_number) action_value
    FROM table1

    Is there a way to return multiple values in my SELECT statement or I'm going about this all wrong?

    Any suggestions?

    Thank you very much!

    Hello

    What you want is a Query of Top - N , what you can do using the ROW_NUMBER analytic function in a subquery, like this:

    WITH     got_rnum     AS
    (
         SELECT     action_seq, action_dt, action_office, action_type, action_value
         ,     ROW_NUMBER () OVER ( ORDER BY  action_date
                                   ,            action_seq
                             ,            action_serial
                           ) AS rnum
         FROM     table2
         WHERE     action_code     = 'AB'
         AND     action_office     LIKE 'E'     -- Is this right?
    )
    SELECT     action_seq, action_dt, action_office, action_type, action_value
    FROM     got_rnum
    WHERE     rnum     = 1
    ;
    

    As written, this returns a single line (at most).
    I suspect you'll actually get a rank for each group , where a group is defined by a value in a table in which you join.
    In this case, add a PARTITION BY clause to the ROW_NUMBER function.
    If post you a small example of data (CREATE TABLE and INSERT statements), I could show you exactly how.
    As I don't have your tables, I'll show you the use of the tables in the scott schema.
    This is a view containing data in the scott.dept table and also to scott.emp, but only for the highest employee in each Department (in other words, the employee whose oldest hire date). If there be a tie for the first hire date, while the candidate with the lowest empno is selected.

    CREATE OR REPLACE VIEW     senior_emp
    AS
    WITH     got_rnum     AS
    (
         SELECT     d.deptno
         ,     d.dname
         ,     e.empno
         ,     e.ename
         ,     e.hiredate
         ,     ROW_NUMBER () OVER ( PARTITION BY  d.deptno
                                   ORDER BY          e.hiredate
                             ,                e.empno
                           ) AS rnum
         FROM     scott.dept     d
         JOIN     scott.emp     e     ON     d.deptno     = e.deptno
    )
    SELECT     deptno
    ,     dname
    ,     empno
    ,     ename
    ,     hiredate
    FROM     got_rnum
    WHERE     rnum     = 1
    ;
    
    SELECT     *
    FROM     senior_emp
    ;
    

    Output:

    .    DEPTNO DNAME               EMPNO ENAME      HIREDATE
    ---------- -------------- ---------- ---------- ---------
            10 ACCOUNTING           7782 CLARK      09-JUN-81
            20 RESEARCH             7369 SMITH      17-DEC-80
            30 SALES                7499 ALLEN      20-FEB-81
    

    Moreover, one of the conditions to the query you posted has been

    action_office     LIKE 'E'
    

    which equals

    action_office     = 'E'
    

    (AS is always equivalent to = if the string that follows AS does not contain the winning cards.)
    Did you mean say that or did you mean something like this:

    action_office     LIKE 'E%'
    

    Instead?

Maybe you are looking for

  • 2011 iMac bricked by the Sierra upgrade

    I have an iMac 27 "2011 that doesn't work anymore after the upgrade of the Sierra. When turns on the iMac, it goes white, then the image of the Apple and a bar progression that moves slowly for about 20 seconds. Then gray screen, then it stops. There

  • Expansion of memory just for my P30

    Can someone tell me if this is the right unit of 512 MB for my P30? -"Corp 512MB SODIMM PC2700 memory." If Basic Help, West a photo on this site: http://www.MPX.no/aspx/prdinfo.aspx?plid=42500 Thank you!

  • Toshiba Tecra A8 monitor not recognized

    Hello I have a Tecra A8 PTA83A 042012 with the video driver Intel (r) Express Chipset driver 945GM. I also have a Toshiba - 1LC2 monitor 21.5 Tekbright PA3719A "The laptop does not detect the resolution correct for this monitor and the closer, I can

  • How to set the spare (next to space) as Alt Gr key?

    I would like to set the spare key (net of the SPACEBAR) to work as Alt Gr is available by Satellite software remap the keyboard?

  • WIN8 registers touch but no reaction from the user interface?

    Anyone having problems with Win8 button when your fingers are clearly registered (Windows gives this feedback from the spherical ripple effect) but the user interface under your finger does nothing? No not the button and so on. I live it with some Ch