我有3個表 1.日曆日期 2.員工詳細 3.衝牀計時 以下表中我需要生成報表來回回月出勤需要SQL SELECT查詢員工的月出勤報告
date
2017-06-01
2017-06-02
2017-06-03
2017-06-04
2017-06-05
2017-06-06
2017-06-07
2017-06-08
2017-06-09
2017-06-10
2017-06-11
2017-06-12
2017-06-13
2017-06-14
2017-06-15
pk EmployeeName
3 IMMAM
4 SRIMAN
5 AJAY
6 Vinay
7 RAGHU
3.衝牀計時
EmployeePk PunchTime
4 2017-06-10 17:49:34.000
4 2017-06-10 18:51:35.000
7 2017-06-10 19:18:37.000
6 2017-06-10 19:18:52.000
5 2017-06-10 19:19:05.000
6 2017-06-12 10:59:34.000
5 2017-06-12 10:59:44.000
5 2017-06-12 16:04:24.000
6 2017-06-12 16:06:48.000
5 2017-06-12 16:08:58.000
5 2017-06-12 16:14:33.000
5 2017-06-13 10:44:06.000
6 2017-06-13 10:44:23.000
4 2017-06-13 10:44:36.000
7 2017-06-13 10:51:22.000
5 2017-06-13 17:45:59.000
6 2017-06-13 17:46:14.000
7 2017-06-13 17:46:26.000
4 2017-06-13 17:47:21.000
5 2017-06-14 10:48:39.000
6 2017-06-14 10:49:04.000
7 2017-06-14 10:49:16.000
4 2017-06-14 10:49:23.000
6 2017-06-14 17:22:34.000
7 2017-06-14 18:23:08.000
4 2017-06-14 18:23:25.000
5 2017-06-14 18:23:32.000
5 2017-06-15 10:44:48.000
4 2017-06-15 10:45:13.000
6 2017-06-15 10:45:32.000
7 2017-06-15 11:03:55.000
5 2017-06-15 11:26:53.000
7 2017-06-15 11:26:56.000
5 2017-06-15 11:29:16.000
5 2017-06-15 11:29:20.000
我需要的報告類似
Calender Date Employee Punchtime IsPresent
10-06-2017 Imama Null No
10-06-2017 SRIMAN 2017-06-10 17:49:34.000 Yes
10-06-2017 AJAY 2017-06-10 19:19:05.000 Yes
10-06-2017 Vinay 2017-06-10 19:18:52.000 Yes
10-06-2017 RAGHU 2017-06-10 19:18:37.000 Yes
11-06-2017 Imama Null NO
11-06-2017 SRIMAN Null NO
11-06-2017 AJAY Null NO
11-06-2017 Vinay Null NO
11-06-2017 RAGHU Null NO
12-06-2017 Imama Null No
12-06-2017 SRIMAN Null No
12-06-2017 AJAY 2017-06-12 10:59:44.000 Yes
12-06-2017 Vinay 2017-06-12 10:59:34.000 Yes
12-06-2017 RAGHU 2017-06-12 10:59:44.000 Yes
而且洙
我開始與像這樣
declare
@month int, @year int, @lastDay int,
@starttime datetime,
@endtime datetime,
@companyPk int,
@employeePk int
set @starttime ='2017-06-01'
set @endtime = '2017-06-30'
set @companyPk =2
set @employeePk =0
select x.calenderdate,y.Pk,y.EmployeeName,y.Ispresent
from
(
(select convert(date, CalendarDate) as calenderdate from ECMSCalendar
where convert(date, CalendarDate) >= @starttime and convert(date, CalendarDate) <[email protected]
) as x
left join
(
select a.Pk,a.EmployeeName,(case when b.IsPresent is null then 'Absent' else b.IsPresent end) as Ispresent from
(
(select emp.Pk,emp.EmployeeName from EmployeeDetails emp where emp.Companypk = @companyPk) as a
left join
(SELECT ed.pk as Pk,ed.EmployeeName as StaffName ,
(case when (DATEDIFF(MINUTE,min(bio.PunchTime),(case when max(bio.PunchTime) = min(bio.PunchTime) then min(bio.PunchTime) else max(bio.PunchTime) end))) < sd.shiftname then 'Absent' else 'Present' end) as IsPresent
--, CONVERT(nvarchar(2), bio.PunchTime) as date
--,count(CONVERT(nvarchar(2), bio.punchtime, 103))as day
from BioMetricPunchDetails bio
left join EmployeeDetails ed on ed.Pk = bio.EmployeePk
left join EmployeeShiftDetails esd on esd.EmployeePk = ed.pk
left join ShiftDetails sd on sd.pk =esd.shiftpk
where --convert (date,bio.punchtime) >= @starttime and convert (date,bio.punchtime) <= @endtime
convert (date,(select min(bpd.PunchTime) from BioMetricPunchDetails bpd where ([email protected] or @employeePk=0))) >= @starttime and convert (date,(select max(bpd.PunchTime) from BioMetricPunchDetails bpd where ([email protected] or @employeePk=0))) <= @endtime
and
ed.Companypk= @companyPk and ([email protected] or @employeePk=0)
--and [email protected]
group by ed.pk, ed.EmployeeName,sd.shiftname) as b
on a.Pk = b.Pk)
) as y
on x.calenderdate = y.Ispresent
)
order by x.calenderdate
請解決該任務謝謝
太棒了!你應該從'SELECT'開始。 –
看看'LEFT JOIN'的功能。 – ZLK
似乎太複雜了...格式化您的查詢也會有很大幫助。 –