我有一个sql server数据库,我正在使用以下查询来获取任何给定员工迟到的出勤日期列表。
SELECT MIN(att.Date)
from Attendance att
where att.Date between '12/01/2011 00:00:00' and '12/31/2011 00:00:00'
and att.EmpID = 4700
GROUP BY EmpID, CONVERT(VARCHAR, att.Date , 111)
having min(CONVERT(VARCHAR, att.Date , 8)) > '09:00:00'
查询返回没有列名的日期列表。 我需要的是计算这个列表的日期。 如何通过此查询完成此操作?
答案 0 :(得分:3)
试试这个:
WITH CTE AS (
SELECT MIN(att.Date) [minDate]
FROM Attendance att
WHERE att.Date between '12/01/2011 00:00:00'
AND '12/31/2011 00:00:00'
AND att.EmpID = 4700
GROUP BY EmpID, CONVERT(VARCHAR, att.Date , 111)
HAVING MIN(CONVERT(VARCHAR, att.Date , 8)) > '09:00:00')
SELECT COUNT(minDate) FROM CTE
Alt而不使用CTE:
SELECT COUNT(minDate) FROM (
SELECT MIN(att.Date) [minDate]
FROM Attendance att
WHERE att.Date between '12/01/2011 00:00:00'
AND '12/31/2011 00:00:00'
AND att.EmpID = 4700
GROUP BY EmpID, CONVERT(VARCHAR, att.Date , 111)
HAVING MIN(CONVERT(VARCHAR, att.Date , 8)) > '09:00:00') AS tbl