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)

Tags: Database

Similar Questions

  • Analytical functions: FIRST vs FIRST_VALUE

    Hello

    Can someone please help me understand the difference between PRIME and FIRST_VALUE in Anaytic functions.

    I tried below 2 queries, but I see the same output. The only difference I see is that the field of the SAL is ordered FIRST_VALUE, but not the FIRST.

    SELECT ename,

    DEPTNO,

    SAL,

    MIN (SAL) keep (dense_rank FIRST

    ORDER BY sal) by (deptno partition) FIRST

    EMP;

    SELECT ename,

    DEPTNO,

    SAL,

    FIRST_VALUE (SAL) over (partition BY deptno arrested by sal) FIRST

    EMP;

    With the help of: Windows 8.1

    Database Oracle 12 c Enterprise Edition Release 12.1.0.1.0 - 64 bit Production

    PL/SQL Release 12.1.0.1.0 - Production

    "CORE 12.1.0.1.0 Production."

    AMT for 64-bit Windows: Version 12.1.0.1.0 - Production

    NLSRTL Version 12.1.0.1.0 - Production

    Hello

    Here is an example of when you can use the FIRST analytic function.

    Say you want the average sal for each Department, but only for the first year (taken from the hiredate column) in the Department (i.e., the column called f in the query below).

    WITH got_hireyear AS

    (

    SELECT deptno and ename, sal, hiredate

    EXTRACT (YEAR FROM hiredate) AS hireyear

    FROM scott.emp

    )

    SELECT deptno, hireyear, hiredate, ename, sal

    AVG (sal) DUNGEON (DENSE_RANK FIRST ORDER BY hireyear)

    COURSES (PARTITION BY deptno

    ) In the FORM f

    FIRST_VALUE (sal) over (PARTITION BY deptno

    ORDER BY hireyear

    ) AS fv

    AVG (sal) over (PARTITION BY deptno

    hireyear

    ), A

    OF got_hireyear

    ORDER BY deptno

    hireyear

    ename

    ;

    Output:

    HIREYEAR ENAME SAL HIREDATE DEPTNO F FV HAS
    ------ ---------- ----------- ---------- ------ --------- ------ ---------
    10 1981 9 June 1981 CLARK 2450 2450 3725,00 3725.00
    10 1981 17 November 1981 KING 5000 3725,00 2450 3725.00
    10 1982 23 January 1982 MILLER 1300 3725,00 2450 1300.00

    20, 1980, 17 December 1980 SMITH 800 800.00 800.00 800
    20, 1981, 3 December 1981 FORD 3000 800.00 800 2987.50
    20, 1981, 2 April 1981 JONES 2975 800.00 800 2987.50
    20, 1987, 23 May 1987 ADAMS 1100 800.00 800 2050.00
    20, 1987, 19 April 1987 SCOTT 3000 800.00 800 2050.00

    30 1981 20 February 1981 ALLEN 1600 1566.67 950 1566.67
    May 30 1981 1st 1981 BLAKE 2850 1566.67 950 1566.67
    December 30 1981 3 1981 JAMES 950 1566.67 950 1566.67
    30 1981 28 - sep - 1981 MARTIN 1250 1566.67 950 1566.67
    30-08 - sep - 1981 1981 TURNER 1500 1566.67 950 1566.67
    30 1981 22 February 1981 WARD 1250 1566.67 950 1566.67

    The analytical FIRST_VALUE function can do (except in the very special case where only 1 row has the lowest hireyear, as in deptno = 20).  AVG analysis can do (except in the very special case that all lines have the same hireyear as in deptno = 30).

  • Analytical functions, model indexes and multiple dimensions.

    I don't understand the notion of analytic functions (windowing clause) in the regulation of a model clause. I discovered an ordinary table as one-dimensional, and I can understand the concept of window, just like a line on a segment line. However with the models, there are several dimensions, so I guess that the window to become a kind of cube, instead of a line segment. But I'm not.

    For example, I have a matrix sparse 2D, with 5 values non-zero:
    select * from field
      where f is not null
    
    X     Y     F
    -----------------
    5     8     X
    6     6     X
    6     8     X
    7     7     X
    7     8     X
    I guess that the following clause of the model
    with t as(
      select *
        from field
       model
         reference old_field
           on
           (
             select * from field
           )
           dimension by (x, y)
           measures     (f)
         main new_field
           dimension by (x, y)
           measures     (cast(f as varchar2(3)) f)
           rules
           (
             f[x,y] = max(old_field.f) over (order by old_field.y range between 1 preceding and 1 following)
           ))
    select * from t
      where f is not null
    to assign for each cell up among its neighbors vertical 2. Just like
    0 0 0      0 X 0
    0 X 0  ->  X X 0
    X 0 0      X X 0
    But the real result is all NULL values.

    Hello

    with a as
    (
    select
    level L
    from
    dual
    connect by level < 4
    )
    
    select
         X
         ,Y
         ,Z
         ,X*3+Y
    
    from
         a          A1
         ,A     A2
    
    model
         dimension by ( A1.L X, A2.L Y )
         MEASURES (0 Z)
         RULES
         (
         Z[X,Y] = COUNT(Z) OVER (ORDER BY X * 3 + Y RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING)
         )
    
    X Y Z X*3+Y
    - - - -----
    1 1 2     4
    1 2 3     5
    1 3 3     6
    2 1 3     7
    2 2 3     8
    2 3 3     9
    3 1 3    10
    3 2 3    11
    3 3 2    12 
    
     9 rows selected 
    

    for x, y = 1.1, there is no previous rank if it is not counted. current line + line = 2
    for x, y = 1, 2, the previous line + line current + more rank = 3
    for x, y = 1.3 the previous line + line current + more rank = 3
    for x, y = 2, 1, the previous line + line current + more rank = 3
    ...
    for x, y = 3, 2 the previous line + line current + more rank = 3
    for x, y = 3, 3 the stored previous + current line = 2. There is no next line.

    What exectly not understand you?

    Kind regards
    Peter

  • 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

  • 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?

  • Nth salary using the analytic function

    I use under function to calculate second highest with empno and deptno salary.

    Is it possible to get the same result with another query without using Assembly only analytical functions condition.using and windows function is possible to get the desired output?

    SELECT e.empno,

    e.DEPTNO,

    tmp. SAL as second_higher_salary

    FROM emp e,.

    (SELECT Empno,

    DEPTNO,

    SAL,

    DENSE_RANK() (PARTITION BY deptno ORDER of sal) AS rnk

    WCP

    ) tmp

    WHERE tmp.deptno = e.deptno

    and tmp.rnk = 2

    EMPNO DEPTNO SAL

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

    7934 10 2450

    7782 10 2450

    7839 10 2450

    7876 20 1100

    7369 20 1100

    7902 20 1100

    7788 20 1100

    7566 20 1100

    7900 30 1250

    7844 30 1250

    7654 30 1250

    7521 30 1250

    7499 30 1250

    7698 30 1250

    7900 30 1250

    7844 30 1250

    7654 30 1250

    7521 30 1250

    7499 30 1250

    7698 30 1250

    Here's my solution:

    Select empno,

    DEPTNO,

    FIRST_VALUE (sal) (PARTITION BY deptno ORDER by sal desc)

    de)

    SELECT EmpNo,

    DEPTNO,

    Decode (DENSE_RANK () OVER (PARTITION BY deptno order by sal desc), 1,-sal, sal) sal

    WCP

    )

    /

    EMPNO DEPTNO FIRST_VALUE (SAL) OVER (PARTITIONBYDEPTNOORDERBYSALDESC)

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

      7782 10 2450
      7934 10 2450
      7839 10 2450
      7566 20 2975
      7876 20 2975
      7369 20 2975
      7788 20 2975
      7902 20 2975
      7499 30 1600
      7844 30 1600
      7654 30 1600
      7521 30 1600
      7900 30 1600
      7698 30 1600
  • Return one row of an analytic function

    Hello

    I pulled the following query:

    Select ID_STORNO, first_value (COD_CONTATTO) ON (Partition of COD_CONTATTO

    order of COD_PRIORITY asc, desc FT_DAT_OPEN_CNT

    rows between unbounded preceding and following unbounded)

    as COD_CONTATTO_LAST

    of WT_STR_ESG_CONTATTO;

    The result is:

    2160603C1-H83J1N
    2160603C8-9FOHXJS
    2258072C1-H83J1N

    But I just need to take the following lines

    2160603C8-9FOHXJS
    2258072C1-H83J1N

    Because for the same value of ID_STORNO, I just need to get a value of COD_CONTATTO (to select the best value using COD_PRIORITY and FT_DAT_OPEN_CNT). What is wrong inside the query? I just use 2 or 3 times the oracle analytic functions.

    Best regards

    As SomeoneElse... you need a where clause clause in order to choose the ones you want.

    To do this, the typical is to select a column that you use for this reason, such as:

    "I want to just the first record in each group..."

    Select id_storno, cod_contatto_last from)

    Select ID_STORNO, first_value (COD_CONTATTO) ON (Partition of COD_CONTATTO

    order of COD_PRIORITY asc, desc FT_DAT_OPEN_CNT

    rows between unbounded preceding and following unbounded)

    as COD_CONTATTO_LAST,

    ROW_NUMBER() OVER (Partition of COD_CONTATTO

    order of COD_PRIORITY asc, desc FT_DAT_OPEN_CNT

    rows between unbounded preceding and following unbounded)

    as rnum

    of WT_STR_ESG_CONTATTO

    )

    where rnum = 1

    /

    I prefer to use rownumber in this case, so I always have a rnum = 1...

    Usually, you must use the same partition/command by as your other folders (it is usually a good idea... maybe not, depending on your needs, however)

  • Analytical function - max_distinct

    Hello world

    (a) I have a request tht returns multiple rows for a student

    (b) to simplify my query, is columns

    Student ID, Academic_Period, student, Expected_Graduation_Date status

    (c) If a student has several status codes
    AS - active
    THE - leave

    He gets 2 rows to display.

    (d) I want to show only the rows

    Maximum preference on the Date of graduation

    (e) so I made this calc:

    MAX_DISTINCT (Expected_Graduation_Date) OVER (PARTITION BY ID.)

    and received the error:
    data types incompatible number got date scheduled

    (f) so I did:

    MAX_DISTINCT (TO_NUMBER (Expected_Graduation_Date)) OVER (PARTITION BY ID.)

    and received the error:
    invalid number

    (g) so I changed to max on alpha field of: student status
    MAX_DISTINCT (student status) OVER (PARTITION BY Id)

    and received the error:
    invalid number

    (h) is the right function to use?
    What's doing FIRST_VALUE.

    Your ideas and your advice would be appreciated, tx, Sandra

    Hello

    You need not use analytical functions. Try to create these calculations to replace the columns where there are different values:

    student_status_last = keep max (student_status) (last dense_rank order by Expected_Graduation_Date)

    Expected_Graduation_Date_last = max (Expected_Graduation_Date)

    Rod West

  • Need help to resolve the query by using analytic functions

    Hello

    I need help to solve this problem, I tried an analytical function but could not solve the problem.

    I have three table as illustrated below the table is filled with a flat file. The records are arranged sequentailly based on the name of the file.

    The first record of the game based on EIN goes to TAB_RCE
    the following records then goes to TAB_RCW
    and last save of the game based on EIN goes to the RCT table

    How can I make groups and
    assign a

    EIN * 12345 * line number * 02, 03, 04 * in the table TAB_RCW and * 05 * in the table TAB_RCT
    EIN * 67890 * line number * 07, 08, 09,10 * in the table TAB_RCW and * 11 * in the table TAB_RCT
    and so on...

    Thank you

    Rajesh

    TAB RCE_--------------------------------------------------------------
    LineNumber EIN FILENAME TYPE

    -----

    01 12345 ABC NCE. TXT
    06 67890 ABC NCE. TXT
    12 76777 ABC NCE. TXT

    -----
    TAB_RCW
    -----
    LineNumber TYPE SSN FILENAME
    -----
    02 22222 ABC RCW. TXT
    03 33333 ABC RCW. TXT
    04 44444 ABC RCW. TXT
    07 55555 ABC RCW. TXT
    08 66666 ABC RCW. TXT
    09 77777 ABC RCW. TXT
    10 88888 ABC RCW. TXT
    13 99998 ABC RCW. TXT
    14 99999 ABC RCW. TXT

    -----
    TAB_RCT
    -----
    NAME OF THE FILE OF TYPE LINENUMBER
    -----
    RCT 05 ABC. TXT
    RCT 11 ABC. TXT
    RCT 15 ABC. TXT
    -----
    SQL> with TAB_RCE as (
      2                   select 'RCE' rtype,'01' linenumber, '12345' EIN,'ABC.TXT' FILENAME from dual union all
      3                   select 'RCE','06','67890','ABC.TXT' from dual union all
      4                   select 'RCE','12','76777','ABC.TXT' from dual
      5                  ),
      6       TAB_RCW as (
      7                   select 'RCW' rtype,'02' linenumber,'22222' ssn,'ABC.TXT' FILENAME from dual union all
      8                   select 'RCW','03','33333','ABC.TXT' from dual union all
      9                   select 'RCW','04','44444','ABC.TXT' from dual union all
     10                   select 'RCW','07','55555','ABC.TXT' from dual union all
     11                   select 'RCW','08','66666','ABC.TXT' from dual union all
     12                   select 'RCW','09','77777','ABC.TXT' from dual union all
     13                   select 'RCW','10','88888','ABC.TXT' from dual union all
     14                   select 'RCW','13','99998','ABC.TXT' from dual union all
     15                   select 'RCW','14','99999','ABC.TXT' from dual
     16                  ),
     17       TAB_RCT as (
     18                   select 'RCT' rtype,'05' linenumber,'ABC.TXT' FILENAME from dual union all
     19                   select 'RCT','11','ABC.TXT' from dual union all
     20                   select 'RCT','15','ABC.TXT' from dual
     21                  )
     22  select  rtype,
     23          last_value(ein ignore nulls) over(partition by filename order by linenumber) ein,
     24          linenumber,
     25          ssn
     26    from  (
     27            select  rtype,
     28                    linenumber,
     29                    ein,
     30                    to_char(null) ssn,
     31                    filename
     32              from  TAB_RCE
     33            union all
     34            select  rtype,
     35                    linenumber,
     36                    to_char(null) ein,
     37                    ssn,
     38                    filename
     39              from  TAB_RCW
     40            union all
     41            select  rtype,
     42                    linenumber,
     43                    to_char(null) ein,
     44                    to_char(null) ssn,
     45                    filename
     46              from  TAB_RCt
     47          )
     48    order by linenumber
     49  /
    
    RTY EIN   LI SSN
    --- ----- -- -----
    RCE 12345 01
    RCW 12345 02 22222
    RCW 12345 03 33333
    RCW 12345 04 44444
    RCT 12345 05
    RCE 67890 06
    RCW 67890 07 55555
    RCW 67890 08 66666
    RCW 67890 09 77777
    RCW 67890 10 88888
    RCT 67890 11
    
    RTY EIN   LI SSN
    --- ----- -- -----
    RCE 76777 12
    RCW 76777 13 99998
    RCW 76777 14 99999
    RCT 76777 15
    
    15 rows selected.
    
    SQL> 
    

    SY.

  • Cannot use analytical functions such as lag/lead in odi components 12 c except in the expression

    Hi I am a beginner of ODI 12 c

    I'm trying to get the last two comments made on the product for a given product id. and load them into a target.

    I have a source table something like

    Product SR_NO comments LAST_UPDATED_TS

    1 good car 2015/05/15 08:30:25

    1 car average 2015/05/15 10:30:25

    Jeep 2 super 2015/05/15 11:30:25

    1 car bad 2015/05/15 11:30:25

    Jeep 2 horrible 2015/05/15 09:30:25

    Jeep 2 excellent 2015/05/15 12:30:25


    I want a target table based on their last timestamp updated as (last two comments)


    SR_NO Comment1 Comment2

    1                             bad                      average

    2 super excellent

    I used the logic below to get records in SQL Developer but in ODI 12 c, I'm not able to do this by mapping a source to the target table by applying analytical functions to the columns in the target table. Can someone help me solve this problem

    SELECT * FROM)

    SELECT SR_NO Comment1, LAG(Comment1,1,) ON Comment2 (SR_NO ORDER BY LAST_UPDATED_TS ASC PARTITION),

    ROW_NUMBER() ON RN (SCORE FROM SR_NO ORDER BY LAST_UPDATED_TS DESC)

    FROM Source_table

    ) M

    WHERE RN = 1

    ;

    UM, I'm afraid that ODI puts the filter too early in the request, if it generates:

    SELECT * FROM)

    SELECT SR_NO Comment1, LAG(Comment1,1,) ON Comment2 (SR_NO ORDER BY LAST_UPDATED_TS ASC PARTITION),

    ROW_NUMBER() ON RN (SCORE FROM SR_NO ORDER BY LAST_UPDATED_TS DESC)

    FROM Source_table

    WHERE RN = 1

    ) M

    ;

    Instead of:

    SELECT * FROM)

    SELECT SR_NO Comment1, LAG(Comment1,1,) ON Comment2 (SR_NO ORDER BY LAST_UPDATED_TS ASC PARTITION),

    ROW_NUMBER() ON RN (SCORE FROM SR_NO ORDER BY LAST_UPDATED_TS DESC)

    FROM Source_table

    ) M

    WHERE RN = 1

    ;

    Even by changing the 'run on Hint"of your component of the expression to get there on the source, the request will stay the same.

    I think the easiest solution for you is to put everything before the filter in a reusable mapping with a signature of output. Then drag this reusable in your mapping as the new source and check the box "subselect enabled."

    Your final mapping should look like this:

    It will be useful.

    Kind regards

    JeromeFr

  • Which analytical function to use?


    Hi gurus,

    DB - Oracle 11 g 2

    I followed the examples of data in the table test_a.

    col1 col2 col3

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

    x                 y                   y

    p                 q                  y

    a                b                   y

    p                q                   y

    t                 r                    y

    p                q                   y

    The col3 column is always 'y '. But here's the data p, q, there is repeated 3 times (duplicate) and if this is the case I want to update only the first recordings like "n" col3 it is to say p, q, n. rest will be as it is.

    I am able to get the row_number() for it but not able to do this.

    Select col1, clo2, clo3 row_number() over (partition by col2) arrested by col1 as test_a r_num

    Would it be possible directly by any analytic function?

    Thank you

    SID

    COL4 is logical...

    Something like that?

    with x as)

    Select col1, col2 ' x' 'y', 'y' col3 col4 1 Union double all the

    Select 'p' col1, col2 'q', 'y' col3 col4 2 Union double all the

    Select 'a' col1, col2 'b', 'y' col3 col4 3 of all the double union

    Select 'p' col1, col2 'q', 'y' col3 col4 4 Union double all the

    Select 't' col1, col2 'r', 'y' col3, col4 5 Union double all the

    Select 'p' col1, col2 'q', 'y' col3 col4 6 double

    )

    ---

    Select * from)

    Select x.*,

    ROW_NUMBER() on rn1 (score of col1, col2, col3 col4 sort),

    ROW_NUMBER() on rn2 (partition by col1, col2, col3 col4 desc sorting)

    x

    )

    where rn1 = 1 and rn2 <> 1;

    Understand the logic and simply change SELECT a query to UPDATE...

  • Using the analytic function

    Oracle 11g Release 2

    I'm assuming that the best solution is the use of analytical functions.

    create table test3
    ( part_type_id  varchar2(50)
    ,group_id      number
    ,part_desc_id  number
    ,part_cmt      varchar2(50)
    )
    /
    
    insert into test3 values( 'ABC123',1,10,'comment1');
    insert into test3 values( 'ABC123',1,10,'comment2');
    insert into test3 values( 'ABC123',2,15,'comment1');
    insert into test3 values( 'ABC123',2,15,'comment2');
    insert into test3 values( 'EFG123',25,75,'comment3');
    insert into test3 values( 'EFG123',25,75,'comment4');
    insert into test3 values( 'EFG123',25,75,'comment5');
    insert into test3 values( 'XYZ123',1,10,'comment6');
    insert into test3 values( 'XYZ123',2,15,'comment7');
    commit;
    
    select * from test3;
    
    PART_TYPE_ID           GROUP_ID PART_DESC_ID PART_CMT
    -------------------- ---------- ------------ --------------------
    ABC123                        1           10 comment1
    ABC123                        1           10 comment2
    ABC123                        2           15 comment1
    ABC123                        2           15 comment2
    EDG123                        25          75 comment3
    EDG123                        25          75 comment4
    EDG123                        25          75 comment5
    XYZ123                        1           10 comment6
    XYZ123                        2           15 comment7
    
    9 rows selected.
    
    Desired output:
    
    PART_TYPE_ID           GROUP_ID PART_DESC_ID PART_CMT
    -------------------- ---------- ------------ --------------------
    ABC123                        1           10 comment1 
    ABC123                        2           15 comment1
    XYZ123                        1           10 comment1
    XYZ123                        2           15 comment2
    
    RULE: where one part_type_id has multiple (2 or more distinct combinations) of group_id/part_desc_id
    
    NOTE: There are about 12 columns in the table, for brevity I only included 4.
    
    
    
    

    Post edited by: orclrunner was updated desired output and rule

    Hello

    Here's one way:

    WITH got_d_count AS

    (

    SELECT part_type_id, group_id, part_desc_id

    MIN (part_cmt) AS min_part_cmt

    COUNT AS d_count (*) OVER (PARTITION BY part_type_id)

    OF test3

    GROUP BY part_type_id, group_id, part_desc_id

    )

    SELECT DISTINCT

    group_id, part_desc_id, part_type_id, min_part_cmt

    OF got_d_count

    WHERE d_count > 1

    ;

    Output:

    GROUP_ID PART_DESC_ID MIN_PART_CMT PART_TYPE_ID

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

    ABC123 1 10 comment1

    ABC123 2 15 comment1

    XYZ123 1 10 comment6

    XYZ123 2 15 comment7

    Analytical functions, such as the COUNTY and MIN, many global versions, in addition, it can give the same results.  Use the analytical versions when each row of output corresponds to exactly 1 row of input and the aggregate and GROUP BY version when each line of output corresponds to a group of lines 1 or more input.  In this issue, each line of output appears to be a group of input lines having the same group_id, part_type_id, and part_desc_id (I'm guessing just, this only has never stated), so I used GROUP BY to get 1 row of output for every input lines.

  • Truncate output of analytical function?

    For example this query:

    Select month, sum (tot_sales) monthly_sales,.

    AVG (Sum (tot_sales)) (any order by month

    between 1 above and 1 below) rolling_avg

    orders

    where year = 2001 and region_id = 6

    Group by month;

    gives me an output which includes several decimal places for the rolling_avg column.

    Is there a way to truncate this? I tried to use the rounded outside the analytical function and surely enough, it didn't work. I can't think otherwise.

    You can use an external selection on the result of this query

    select trunc(rolling_avg) from
    ( rolling_avg query);
    
  • 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

  • without analytic function

    Hello experts.

    I have data similar to what follows below

    create table t1
    (
      id number(30),
      description varchar(4000)
    
    
    );
    
    insert into t1 values (1, 'zone');
    insert into t1 values (2, 'small');
    
    
    create table t2
    (
       id number(30),
       place varchar(4000),
       info varchar(4000)
    
    );
    
    insert into t2 values (1, 'USA', 'Class U');
    insert into t2 values (1, 'Mexico', 'Class M');
    insert into t2 values (2, 'Germany', 'Class G');
    

    I need help with something similar to what follows below without using any analytic function

    Description of the ID info Place

    1 box USA class U

    Mexico 1 M class

    2 small Germany class G

    Any help is appreciated. Thank you

    Hello

    user13328581 wrote:

    ... I use an older version of oracle. Oracle 7.

    Normally, your developers are older than your software.

    You should be able to do what you want with a self-join on t2; a copy (d) should be displayed, and the other copy (c) contains all related values you need for comparison.

    SELECT t2d.id

    DECODE (t2d.place

    MAX (t2c.place)

    t1.description

    ) AS description

    t2d.place

    t2d.info

    FROM t1

    , t2 t2d - display

    t2 t2c - compare

    WHERE t1.id = t2d.id

    AND t2d.id = t2c.id

    GROUP BY t1.description

    t2d.id

    t2d.place

    t2d.info

    ORDER BY t2d.id

    t2d.place DESC

    ;

    Output:

    ID DESCRIPTION PLACE INFO

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

    1 box USA class U

    Mexico 1 M class

    2 small Germany class G

    I've tested this in Oracle 11, but it should work in Oracle 7.

    If this isn't the case, you may need to create a view.

Maybe you are looking for

  • Satellite P200 - unable to connect to the WLAN router

    I have a Satellite P200 and that you have upgraded from Vista to Windows 7. The wireless is working and to see available networks, but trying to connect it does not ask the WEP key, it just says: he has a connection problem and (possibly) says that i

  • How to fix error 8024d00e

    I want to update my windows, but cant it always shows error 8024d00e what is it and how can I fix

  • Site2Site and remote VPN

    I have a site2site between PIX506 and 877 router VPN. Site A has PIX506 and Site B router a in 877. I configured site2site VPN and it worked fine. I also configured remote VPN on PIX 506 so that the remote user can access A site. But when I configure

  • Issue of Compression on BB Curve blackBerry Smartphones

    Hello I have the CEO who said he was able to compress all attachments in an email before you send it. Example, it would have a 100K jpeg and was able to provide compress 50K. He claims that he didn't this option more since I changed the policy to the

  • Can we define a field value of contact with the CRM campaign settings ID campaign?

    HelloCan we define a field value of contact with the ID of CRM campaign?For example: LAST campaign SFDC ID (field of contact) = CRM campaign (campaign) No.