OR PROCEDURE p_emp_work_detail the REPLACE the CREATE (p_month VARCHAR2) the AS / * ************************************** ****************************** program features: create a stored procedure to achieve separate commuting time employees; determine whether employees are late , leave early, overtime; developer: XXX development time: 2017/09/22 source table: ods_emp_work temporary table: temp_emp_work target table: emp_work_detail ********************* *********************************************** * / str VARCHAR2 ( 8000 ); the BEGIN --- create a temporary table Execute immediate ' Create table temp_emp_work ( U_ID Number ( 20 is ), u_name VARCHAR2 (50), u_time varchar2(50), sign_in date, sign_out date, sign_more date )'; --给临时表插入数据 str:= 'insert into temp_emp_work(u_id,u_name,u_time,sign_in,sign_out,sign_more) select u_id, u_name, to_char(datetime,''yyyymm''), max(case when r=1 then datetime else null end) sign_in, max(case when r=2 then datetime else null end) sign_out, max(case when r=3 then datetime else null end) sign_more from( select u_id, u_name, datetime, row_number()over(partition by u_id,u_name,to_char(datetime,''yyyymmdd'')order by 1) r from ods_emp_work where to_char(datetime,''yyyymm''):=1) group by u_id, u_name, TO_CHAR (datetime, the data does not satisfy the working time is updated to empty'' YYYYMM '' ) ' ; the commit; - SELECT * from temp_emp_work - adjusting Diameter - a punch card records - a work recorded punch - a punch out recording (after 5:00 to commute) Execute immediate ' Update temp_emp_work SIGN_OUT = sign_in SET WHERE SIGN_OUT IS null and TO_CHAR (sign_in, '' HH24: mi The '' )> '' 1700 '' ' ; -where immediate Execute ' Update temp_emp_work = null SET sign_in TO_CHAR (sign_in, ' ' HH24: mi The ' ' )> ' ' 1700 ' '' ; the commit; --- three punch card records - twice to work, after work time recorded - a work twice from work record Execute immediate ' Update temp_emp_work SET SIGN_OUT = sign_more WHERE sign_more IS Not null ' ; the commit; - determining whether late, leave early, overtime - create the target table / * Create table emp_work_detail ( U_ID Number (20 is), u_name VARCHAR2 (50), u_month VARCHAR2 (50), sign_in DATE, SIGN_OUT DATE, is_late Number (20), is_early number(20), is_overtime number(20) ); */ --给目标表中插入数据 execute immediate 'truncate table emp_work_detail'; execute immediate 'insert into emp_work_detail(u_id,u_name,u_month,sign_in,sign_out,is_late,is_early,is_overtime) select u_id, u_name, substr(u_time,1,6), sign_in, sign_out, case when to_char(sign_in,''hh24mi'')>''0901'' then 1 else 0 end is_late, case when to_char(sign_out,''hh24mi'')<''1730'' then 1 else 0 end is_early, case when to_char(sign_out,''hh24mi'')>''2000'' then 1 else 0 end is_over_time from temp_emp_work'; commit; execute immediate 'drop table temp_emp_work'; --select * from emp_work_detail end;