Basic usage of oracle's ROW_NUMBER() OVER function

 Reprinted from: http://www.cnblogs.com/icebutterfly/archive/2009/08/05/1539657.html

 语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)

 

row_number() OVER (PARTITION BY COL1 ORDER BY COL2) means grouping according to COL1 and sorting according to COL2 within the grouping, and the value calculated by this function represents the order number after sorting within each group (consecutive and unique within the group)

 

Example 1: There are numbers such as 1, 2, 3 in row_number

SElECT row_number() over(partition by b.agency_item_id, c.processinst_id order by c.createdate desc) row_num,
                    b.agency_item_id,
                    b.agency_id,
                    d.*
               FROM tb_bgt_dept_status b
               join tb_bgt_ref_dept_wf c
                 on b.id = c.bgt_wf_id
               join v_wf_working d
                 on c.processinst_id = d.PROCINSTID
              where b.annual = 2016

Note: The diagram of Example 1 is shown below

 

 

 

Example 2: Both are numbers 1

SElECT row_number() over(partition by b.agency_item_id, c.processinst_id order by c.createdate desc) row_num,
                    b.agency_item_id,
                    b.agency_id,
                    d.*
               FROM tb_bgt_dept_status b
               join tb_bgt_ref_dept_wf c
                 on b.id = c.bgt_wf_id
               join v_wf_working d
                 on c.processinst_id = d.PROCINSTID
              where b.annual = 2016

)where row_num=1

Note: Example 2 is shown in Figure 2

 

 

Second: whether the query appears once and how many times the query appears

    select count(agency_id) ,t.agency_id  from tb_plan_info t group by t.agency_id

 

 

-------------------------------------------------- ------------------------------------- The following are two sql comparisons, the effect is the same.

Two ways to find the current largest link of the current instance in the process (see the flowchart)

sql1: As follows: Group by process instance and operation process user. Find the last process currently operating in this process instance

  SELECT VW.*
 FROM V_WF_WORKDONE VW,
 (SELECT PROCINSTID, USERID, MAX(TASK_END_TIME) MAXENDDATE
  FROM V_WF_WORKDONE
  GROUP BY PROCINSTID, USERID) VT
  WHERE VW.TASK_END_TIME = VT.MAXENDDATE
  AND VW.PROCINSTID = VT.PROCINSTID
  AND VW.USERID = VT.USERID

 

 

sql2: as follows: first group by process instance and user, then sort by time, and finally take the first data

SELECT row_number() over(partition by v.PROCINSTID, v.USERID order by v.TASK_END_TIME desc) row_num,
       v.TASKINSTID,
       v.USERID,
       v.PROCINSTID,
       v.taskdef,
       v.TASK_END_TIME
  FROM v_wf_workdone v where row_num=1

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327106959&siteId=291194637