oracle deduplication, acquiring the latest first data fetch

The most recent project involves a sql statement to get a table in the latest piece of data encoded in the same work order, in line to see a function perfect solution to this problem, the following is the content of points:

Question: Is there a project in device detection information table DEVICE_INFO_TBL, each device will have a detection information every day, and now you need to retrieve the latest detection information for each device from the table. That is device_id field can not be repeated, eliminate device_id field duplicate records, and device_id corresponding detection information test_result date.

 

Solution: use Oracle 's row_number () over functions to solve the problem.

Resolution process:

 1. See Table duplicate records

select

    t.id,

    t.device_id,

    t.update_dtm,

    t.test_result

from DEVICE_INFO_TBL t

Check duplicate data

2. Mark duplicate records

select

    t.id,

    t.device_id,

    t.update_dtm,

    t.test_result,

    row_number() OVER(PARTITION BY device_id ORDER BY t.update_dtm desc) as row_flg   

from DEVICE_INFO_TBL t

Mark duplicate records

 

3. Filter duplicate data, to get the latest recording

select

    temp.id,

    temp.device_id,

    temp.update_dtm,

    temp.test_result

from (

         select

             t.id,

             t.device_id,

             t.update_dtm,

             t.test_result,

             row_number() OVER(PARTITION BY device_id ORDER BY t.update_dtm desc) as row_flg   

          from DEVICE_INFO_TBL t ) temp

where temp.row_flg  = '1'

Filter duplicate data





 

row_number () OVER (PARTITION BY COL1 ORDER BY COL2) shows a group COL1, COL2 ordered within a packet according to, and this value is calculated by the function represented by (continuous inner set of unique) sequence number after each internal sorting.

  Rownum differs in that: when sorting using rownum added to the result set is pseudo-column rownum then sort the sort function clause is contained in the first sorting line number recalculation.

  ROW_NUMBER () and rownum similar, powerful point (1 can be sorted from the opening in each packet).

  Rank () is skipped sorted, the next step is the fourth time two second (likewise in each group).

  dense_rank () l is a continuous sort, still followed by third place when two second place. In contrast row_number is no duplicate values.

  LAG (arg1, arg2, arg3):
arg1 is returned from the other rows expression
arg2 is the offset of the current row partition to be searched. Is a positive offset, back when a previous number of rows retrieved.
arg3 value is returned when the number is outside the range represented arg2 packet.

 

 

Reference article: https://blog.csdn.net/nux_123/article/details/45037719

Published 118 original articles · won praise 59 · views 490 000 +

Guess you like

Origin blog.csdn.net/u012255097/article/details/87490712