简体   繁体   English

用于搜索日期的SQL查询

[英]SQL query for searching the dates

I have written a SQL query which search the employee ATTENDANCE Table for the persons who are absent. 我编写了一个SQL查询,在查询员工ATTENDANCE表中查找缺席的人员。 But how can I show the dates I mean the date of the absent period? 但是,我怎样才能显示我所说的缺席期的日期? It works fine except the date. 除日期外它工作正常。 I want to show what's the day of that absent period? 我想表明那个缺席时期的那一天是什么时候? I am unable to show the date. 我无法显示日期。

Sample: 样品:

BadgeNumber - EmployeeName - Absent Date
10042 - Mr. Erik - 2014-07-01

Code: 码:

SELECT  SMEmployee.BadgeNumber,SMEmployee.EmployeeFullName,SMDepartment.DepartmentName
    FROM SMEmployee
    LEFT OUTER JOIN SMDepartment 
    ON SMDepartment.DepartmentID=SMEmployee.DepartmentID    
    WHERE EmployeeID NOT IN (
    SELECT empCheckInOut.USERID 
    FROM empCheckInOut 
    WHERE convert(date,CHECKTIME)='2014-07-01')

It seems that you capture the days employee is present in 'empCheckInOut' table. 您似乎在'empCheckInOut'表中捕获员工所在的日期。

You need to construct temp table with all the dates lets say of 1 month and then minus the dates that are captured in 'empCheckInOut' table. 您需要构建临时表,其中包含1个月的所有日期,然后减去'empCheckInOut'表中捕获的日期。

eg #temp will have 06-01-2014, 06-02-2014, 06-03-2014, ... (all 30 days of Jun) 例如#temp将有06-01-2014,06-02-2014,06-03-2014,...(6月的所有30天)

CheckInOut will have 06-01-2014, 06-03-2014,... (emp is absent on 2nd Jun, so total 29 days of Jun captured here) CheckInOut将于06-01-2014,06-03-2014,...(emp在6月2日缺席,因此在这里捕获的总共29天)

Then, subtract one from the other and you will get dates when employee is absent. 然后,从另一个中减去一个,您将获得员工缺席的日期。

Normally to check for the lack of existence of a record (as you are doing here for attendence, checking the employee did not check in / out on a day) it is more efficient to use a LEFT OUTER JOIN and the check for NULL in one of the fields in the WHERE clause (ie, a LEFT JOIN where no row found). 通常要检查记录是否存在(正如你在这里注意的那样,检查员工没有在一天检入/退出),使用LEFT OUTER JOIN和检查NULL是否更有效率WHERE子句中的字段(即,没有找到行的LEFT JOIN)。

However this also won't give you the date. 但是这也不会给你日期。

I presume that you want to check a selection of dates, and this need to pull through the date they were absent. 我认为你想要检查选择的日期,这需要了解他们缺席的日期。

As such maybe use a series of unioned simple queries to get the dates, CROSS JOIN that to your employee table and LEFT JOIN it to the empCheckInOut table. 因此,可以使用一系列联合简单查询来获取日期,将CROSS JOIN连接到您的employee表,并将LEFT JOIN连接到empCheckInOut表。 This way you can put out the date. 这样你就可以把日期搞定了。

SELECT  SMEmployee.BadgeNumber,SMEmployee.EmployeeFullName,SMDepartment.DepartmentName, subDate.aDate
FROM SMEmployee
CROSS JOIN (SELECT '2014-07-01' AS aDate UNION SELECT '2014-06-30') subDate
LEFT OUTER JOIN SMDepartment 
ON SMDepartment.DepartmentID=SMEmployee.DepartmentID    
LEFT OUTER JOIN empCheckInOut
ON SMEmployee.EmployeeID = empCheckInOut.USERID 
AND convert(date,CHECKTIME) = subDate.aDate
WHERE empCheckInOut.USERID IS NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM