2011-04-07 135 views
0

的SQL查詢時需要幫助我有2和表1是EMployeeMaster,另一個是出席在時間管理系統

***EmployeeMaster*** 

EmployeeId EmployeeName DepartmentId 
1    ABC   1 
2    XYZ   2 
3    PQR   2 
4    WXY   1 

現在我有另一臺考勤

***Attendance*** 

AttendanceId EmployeeId  Date     InTime  OutTime 
1   1   2011-04-04 00:00:00 10:00 AM  6:30 PM 
2   2   2011-04-04 00:00:00 09:45 AM  7:10 PM 

一旦員工進來辦公室,把他的手指在設備上,他的入場將會與InTime,EMployeeId和Date一起進入考勤表。

因此,沒有上任的員工,他的入場將不會出現在考勤表中。

現在我想要生成每日報告..它應該顯示公司的所有員工以及他們的銀泰/ outTime出勤日期。 所有缺席的員工也應該顯示在本報告中。

所以我想:

EmployeeId EMployeeName DepartmentId Date     InTime OutTime 
1    ABC   1    2011-04-04 00:00:00 10:00 AM 6:30 PM 
2    XYZ   2    2011-04-04 00:00:00 09:45 AM 7:10 PM 
3    PQR   2    NULL/-    NULL/- NULL/- 
4    WXY   1    NULL/-    NULL/- NULL/- 

你能告訴我什麼都要查詢???

回答

1

你應該使用一個左外連接。

declare @E table (EmployeeId int, EmployeeName varchar(50), DepartmentId int) 
declare @A table (AttendanceId int, EmployeeId int, [Date] date, InTime time, OutTime time) 

insert into @E values 
(1,    'ABC',   1), 
(2,    'XYZ',   2), 
(3,    'PQR',   2), 
(4,    'WXY',   1) 

insert into @A values 
(1,   1,   '2011-04-04 00:00:00', '10:00 AM',  '6:30 PM'), 
(2,   2,   '2011-04-04 00:00:00', '09:45 AM',  '7:10 PM') 

select 
    E.EmployeeId, 
    E.EmployeeName, 
    E.DepartmentId, 
    A.[Date], 
    A.InTime, 
    A.OutTime 
from @E as E 
    left outer join @A as A 
    on E.EmployeeId = A.EmployeeId and 
     A.[Date] = '2011-04-04 00:00:00' 

結果

EmployeeId EmployeeName          DepartmentId Date  InTime   OutTime 
----------- -------------------------------------------------- ------------ ---------- ---------------- ---------------- 
1   ABC            1   2011-04-04 10:00:00.0000000 18:30:00.0000000 
2   XYZ            2   2011-04-04 09:45:00.0000000 19:10:00.0000000 
3   PQR            2   NULL  NULL    NULL 
4   WXY            1   NULL  NULL    NULL 

編輯1

如果需要一個月爲此,我需要使用某種數字表或日曆表。在這裏,我已經使用了一個CTE來構建使用@FromDate和@ToDate

declare @E table (EmployeeId int, EmployeeName varchar(15), DepartmentId int) 
declare @A table (AttendanceId int, EmployeeId int, [Date] date, InTime time, OutTime time) 

insert into @E values 
(1, 'ABC', 1), 
(2, 'XYZ', 2), 
(3, 'PQR', 2), 
(4, 'WXY', 1) 

insert into @A values 
(1, 1, '2011-04-02', '04:00 AM', '4:30 PM'), 
(2, 2, '2011-04-02', '05:00 AM', '5:30 PM'), 
(3, 1, '2011-04-03', '06:00 AM', '6:30 PM'), 
(4, 2, '2011-04-03', '07:00 AM', '7:30 PM'), 
(5, 1, '2011-04-04', '08:00 AM', '8:30 PM'), 
(6, 2, '2011-04-05', '09:00 AM', '9:10 PM') 

-- Set FromDate to first day of month 
declare @FromDate date = '20110401' 
-- Set ToDate to last day of month 
declare @ToDate date = '20110405' 

-- Create cte with all dates between FromDate and ToDate 
;with cteCal as 
(
    select @FromDate as [Date] 
    union all 
    select dateadd(d, 1, [Date]) as [Date] 
    from cteCal 
    where [Date] < @ToDate 
) 
select 
    E.EmployeeId, 
    E.EmployeeName, 
    E.DepartmentId, 
    C.[Date], 
    A.InTime, 
    A.OutTime 
from cteCal as C 
    cross join @E as E 
    left outer join @A as A 
    on E.EmployeeId = A.EmployeeId and 
     C.[Date] = A.[Date] 
order by C.[Date], E.EmployeeName 
option (maxrecursion 0) 
+0

但現在嘗試使用where子句和通過日期作爲where子句.... – 2011-04-07 09:19:56

+0

@Kishan - Not in其中cla使用,加入。更新了答案。 – 2011-04-07 09:20:55

+0

謝謝..它適用於我... – 2011-04-07 09:39:58

0

應該是這樣的:

select 
    EmployeeId, 
    EmployeeName, 
    DepartmentId, 
    Date, 
    InTime, 
    OutTime 
from EmployeeMaster em 
left join Attendance a on em.EmployeeId=a.EmployeeId and Date='2011-04-04 00:00:00' 
+0

沒有得到員工誰是不存在的..所以僱員-3&4沒有結果得到.... – 2011-04-07 09:12:51

+0

好日曆..你應該現在得到他們 – 2011-04-07 09:14:11

0
select E.EmloyeeId, EmployeeName, DepartmentId, Date, InTime, OutTime 
from EmployeeMaster as e 
     LEFT JOIN Attendance as a ON a.EmloyeeId = e.EmloyeeId 
+0

看到這個作品...但是當我通過合適的日期我沒有得到缺席的員工... 'select E.EmployeeId,EmployeeName,DepartmentId,Date,InTime,OutTime from EmployeeMaster as e LEFT JOIN出席作爲a.EmployeeId = e.EmployeeId其中a.Date ='2011-04-04'' – 2011-04-07 09:16:42