当我尝试执行
时,此SQL查询失败"当SUM(CONVERT(十进制(10,2),DATEDIFF(第二,t.Start,t.Finish)/ 3600.0))> 8"
在我的案例陈述中。
SELECT e.EmployeeNumber, e.FirstName, e.LastName, cast(t.DateWorked as date) as DateWorked,
SUM(CONVERT(decimal(10,2),DATEDIFF(second, t.Start, t.Finish ) / 3600.0)) as HoursWorked,
SUM(CASE
-- #Check for overtime# HoursWorked > DailyHours
WHEN SUM(CONVERT(decimal(10,2),DATEDIFF(second, t.Start, t.Finish ) / 3600.0)) > 8
THEN
1
ELSE
0
END) as moneyEarned
FROM Timesheets t, Employees e
WHERE cast(t.DateWorked as date) BETWEEN @datefrom AND @dateto
AND t.IsDeleted = 0
AND t.EmployeeId = e.EmployeeId
AND t.FarmId = @farmid
GROUP BY e.EmployeeNumber, e.FirstName, e.LastName, cast(t.DateWorked as date)
有没有办法可以访问" HoursWorked"作为我的Case语句中的变量?
目前我正在获得"无法对包含聚合或子查询的表达式执行聚合函数。"错误。
任何关于我如何以不同方式处理这一点的指示都将非常感激。
答案 0 :(得分:1)
sum()
内有sum()
,但不允许这样做。据推测,你打算:
SELECT e.EmployeeNumber, e.FirstName, e.LastName,
cast(t.DateWorked as date) as DateWorked,
SUM(CONVERT(decimal(10,2),DATEDIFF(second, t.Start, t.Finish ) / 3600.0)) as HoursWorked,
(CASE -- #Check for overtime# HoursWorked > DailyHours
WHEN SUM(CONVERT(decimal(10,2),DATEDIFF(second, t.Start, t.Finish ) / 3600.0)) > 8
THEN 1 ELSE 0
END) as moneyEarned
. . .
您还应该学会使用正确的显式join
语法。