Oracle SQL - DENSE_RANK

后端 未结 4 1870
抹茶落季
抹茶落季 2021-01-19 18:13

I have a Client data table, selected columns of which are shown below:

Row_ID  Client_ID  Status_ID  From_date           To_date
  1     123456       4               


        
4条回答
  •  爱一瞬间的悲伤
    2021-01-19 18:58

    As I understood, this is what you need:

    select client_ID, status_id, from_date, to_date, 
           sum(start_of_group) over (order by client_ID, from_date) + 1 rank
      from (SELECT c.client_ID, c.status_id, c.from_date, c.to_date,
                   case when lag(c.client_ID, 1, c.client_ID) over (order by c.client_ID, c.from_date) = c.client_ID 
                         and lag(c.status_id, 1, c.status_id) over (order by c.client_ID, c.from_date) = c.status_id
                        then 0 else 1 end start_of_group
              FROM client c)
     order by client_ID, from_date
    

    SQLFiddle

提交回复
热议问题