我的查询如下:
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
我需要在C#应用程序中使用它,但我不能只使用查询,因为它说查询必须以select或from关键字开头。我需要此查询才能在HQL
中使用。请帮忙。
答案 0 :(得分:1)
您必须重写查询。
在结果中我们根本不关心日期,我们对一般结果感兴趣,因此我们可以从选择中删除日期并将其替换为EmpId *。
SELECT EmpID
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(EmpID) FROM Attendance WHERE EmpID IN (
SELECT EmpID
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')
);
第二个查询仅重新表达CTE表达式,在大多数情况下只是语法糖。将其转换为HQL应该不是问题。
*我认为EmpID是唯一的。
答案 1 :(得分:1)
最后它适用于这些
SELECT count(att.AttDate)
from Attendance att
where att.Employee.EmployeeCD = "+ empCD +"
and att.AttDate IN
(SELECT MIN(att.AttDate)
from Attendance att
where att.AttDate between '" + startDate.Date + "' and '" + endDate.Date + "'
and att.Employee.EmployeeCD = " + empCD + "
GROUP BY EmployeeCD, CONVERT(VARCHAR,att.AttDate, 111)
having min(CONVERT(VARCHAR, att.AttDate, 8)) > '08:10:00')