Home > database > Oracle query, how to take the first data for continuous value of the time
Oracle query, how to take the first data for continuous value of the time
Time:09-18
Excuse me everybody, grouped by ID, in reverse chronological order, a new type of article value is 2, look for continuous data for the 2, when the type value of discontinuity is a 2, 2 of the first data of the time; If the latest article for 2, do not find out the ID, the chart data, for example, how to write a SQL statement to be able to make the final query result for the data in the red box? Thank you ~
CodePudding user response:
Thinking: 1, first remove each well_id maximum time and type='1' data Select * from table where well_id in (select b.w ell_id from table b, (select well_id, Max time (time) from the table group by well_id) a Where b.t ype='2' and b.t ime=a.t ime and b.w ell_id=aleem walji ell_id ) 2, take each well_id maximum time and type='1' data and the data of rownum 3, the rownum above 1 data is the data we need
CodePudding user response:
with tab1 as ( Select 1 id, 1 type, 11 word from dual union all Select id 1, type 2, 10 word from dual union all Select id 1, type 2, 9 word from dual union all Select id 1, type 2, 8 word from dual union all Select id 1, type 2, 7 word from dual union all Select 1 id, type 1, 6 word from dual union all Select 2 id, type 2, 10 word from dual union all Select 2 id, 1 type, nine word from dual union all Select 3 id, type 2, 10 word from dual union all Select 3 id, type 2, 9 word from dual union all Select 3 id, type 1, 8 word from dual ) Tab2 as ( Select a t1. Id, T1. Type, T1. Word, Row_number () over (partition by t1. Id order by t1. The word desc) rn The from tab1 t1 The order by t1. Id, word desc ) Select distinct T1. Id, First_value (t1) word) over (partition by t1. The id order by level desc) time The from tab2 t1 Start with t1. Rn=1 Connect by the prior t1. Id=t1. Id And the prior t1. Rn + 1=t1. Rn And not (t1) type!=2 and the prior t1. Type=2) ;