【问题标题】:Need help in sql query of Time Management system在时间管理系统的sql查询中需要帮助
【发布时间】:2011-04-07 09:06:14
【问题描述】:

我有两张桌子,一张是 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。

所以没有进入办公室的员工,他的条目将不存在于出勤表中。

现在我想生成每日报告..它应该按日期显示公司所有员工的出勤率以及他们的 Intime/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/-

你能告诉我应该查询什么吗???

【问题讨论】:

    标签: sql sql-server-2008


    【解决方案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)
    

    【讨论】:

    • @Kishan - 不在 where 子句中,在 join 中。更新了答案。
    • 我有 1 个问题..我希望你能帮助我...在当前情况下,我将获得月度报告...我不会知道谁缺席的日期。 ..所以我有什么办法可以做到这一点...
    • @Kishan - 更新答案。这可能应该在另一个问题中,所以你可以获得比我更多的关于如何做到这一点的建议。
    【解决方案2】:

    应该是这样的:

    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'
    

    【讨论】:

    • 没有得到缺席的员工..SO EmployeeId-3 & 4 没有得到结果..
    • 好的 .. 你现在应该得到它们
    【解决方案3】:
    select E.EmloyeeId, EmployeeName, DepartmentId, Date, InTime, OutTime
    from   EmployeeMaster as e
           LEFT JOIN Attendance as a ON a.EmloyeeId = e.EmloyeeId
    

    【讨论】:

    • 看到这个作品......但是当我通过特定日期时,我没有得到缺席的员工......select E.EmployeeId, EmployeeName, DepartmentId, Date, InTime, OutTime from EmployeeMaster as e LEFT JOIN Attendance as a ON a.EmployeeId = e.EmployeeId where a.Date='2011-04-04'
    猜你喜欢
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 2016-07-04
    • 2016-07-26
    • 1970-01-01
    相关资源
    最近更新 更多