sql - Flag first two raw that has a value in a group -
first not expert in sql. that's why seeking help. have oracle view contain employees attendance records including late attendance days. columns :
employee number ,date, in-time, out-time, late_arrival (if late getting marked '1')
i want mark each employees fist 2 late arrivals of month "g". query.
please me this.
select l.emp_no,l.account_date,l.in_time,l.out_time,l.late_coming_15 late_arrival, (case when l.late_coming_15 = 1 , row_number() on (partition trunc(l.account_date),l.late_coming_15 order l.in_time ) <= 2 'g' end) flag late_earlydep l trunc(l.account_date) >= to_date('&date_from','dd/mm/yy') , trunc(l.account_date) <=to_date ('&date_to','dd/mm/yy') order 1,2`
but still not getting required result. example employee number 0005's flaged days should 02/03/2017 & 06/03/2017 . have uploaded result below google drive link
result https://drive.google.com/open?id=0b6xw1exelyg7m3dtbmjuek5odg8
this logic:
row_number() on (partition trunc(l.account_date), l.late_coming_15 order l.in_time)
is enumerating rows each day, not each month. if want first 2 per month, need change partition by
clause:
row_number() on (partition l.emp_no, trunc(l.account_date, 'mon'), l.late_coming_15 order l.in_time)
Comments
Post a Comment