1

我有一个员工表和一个假期表,其中每一行代表他们休假的一天。

我想让所有休假天数超过其最大休假天数的员工。

employees:
employeeId
maxVacationDays

vacations:
employeeId
date_vacation_day_taken

我现在的查询:

SELECT e.*
FROM employees e
         INNER JOIN vacations v ON (v.employeeId = e.employeeId)
WHERE e.isActive = true AND ( ???? > e.maxVacationDays)

因此,我需要从该employeeId 的假期表中获取行数。

我需要一个子查询吗?

(使用sql2000)

4

4 回答 4

2

试试这个:

SELECT e.employeeId, v.maxVacationDays
FROM employees e
         INNER JOIN vacations v ON (v.employeeId = e.employeeId)
WHERE e.isActive = true
GROUP BY e.employeeId, v.maxVacationDays
HAVING COUNT(v.employeeId) > v.maxVacationDays
于 2012-06-27T15:40:03.260 回答
1
SELECT e.EmployeeId
        ,COUNT(v.date_vacation_day_taken) TotalDaysTaken
        ,e.maxVacationDays MaxDaysAllowed
FROM Employees e
    INNER JOIN vacations v 
        ON v.employeeId = e.employeeId
WHERE e.isactive = true
GROUP BY e.EmployeeId, e.maxVacationDays, e.isactive
HAVING COUNT(v.date_vacation_day_taken) > e.maxVacationDays
于 2012-06-27T15:32:22.423 回答
1

试试这个:

SELECT * FROM 
(
    SELECT e.employeeId, e.maxVacationDays, 
           SUM(v.date_vacation_day_taken) 'dayscount'
    FROM employees e
    INNER JOIN vacations v ON (v.employeeId = e.employeeId)
    GROUP BY e.employeeId, e.maxVacationDays
    WHERE e.isactive = true
) sub
WHERE sub.dayscount > e.maxVacationDays)
于 2012-06-27T15:35:21.200 回答
1

也许是这样:

select e.employeeId, e.maxVacationDays, count(v.*) as vacationDaysTaken
from employees e
    join vacations v on e.employeeId = v.employeeId
group by e.employeeId, e.maxVacationDays
having e.maxVacationDays < count(v.*)
于 2012-06-27T15:36:34.463 回答