IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[kaoqing]') AND type = 'V')
DROP VIEW [dbo].[kaoqing]
GO


CREATE  view  [dbo].[kaoqing] as
(
select * from (select id,number,name,departmentname,right(CONVERT(varchar(100), date, 23),5) date,(case when shiftname='' then '未排班' else shiftname end) 排班,
(case when late=0 then '' else cast(late as nvarchar(5))+'分' end) 时长,
'迟到' 类型,date date1
from EHR.[EHR821_2016Qsy].[dbo].EHRAbnormaDay 
where departmentname!='寸滩库房' and (late!=0 or leave!=0 or absenteeism!=0) and left(convert(char,date,23),7) = left(convert(char,dateadd(DD,-1,getdate()),23),7)  and convert(char,date,23)<convert(char,GETDATE(),23) ) t  where t.时长  !=''

union all

select * from(select id,number,name,departmentname,right(CONVERT(varchar(100), date, 23),5) date,(case when shiftname='' then '未排班' else shiftname end) 排班,
(case when leave=0 then '' else cast(leave as nvarchar(5))+'分' end) 时长,
'早退' 类型,date date1
from EHR.[EHR821_2016Qsy].[dbo].EHRAbnormaDay 
where departmentname!='寸滩库房' and (late!=0 or leave!=0 or absenteeism!=0) and left(convert(char,date,23),7) = left(convert(char,dateadd(DD,-1,getdate()),23),7)  and convert(char,date,23)<convert(char,GETDATE(),23) ) t where t.时长  !=''
union all

select *  from (select id,number,name,departmentname,right(CONVERT(varchar(100), date, 23),5) date,(case when shiftname='' then '未排班' else shiftname end) 排班,
(case when absenteeism=0 then '' else cast(absenteeism/60 as nvarchar(5))+'时' end) 时长,
'矿工' 类型,date date1
from EHR.[EHR821_2016Qsy].[dbo].EHRAbnormaDay 
where departmentname!='寸滩库房' and (late!=0 or leave!=0 or absenteeism!=0) and left(convert(char,date,23),7) = left(convert(char,dateadd(DD,-1,getdate()),23),7)  and convert(char,date,23)<convert(char,GETDATE(),23) )  t where t.时长  !=''

)