我有一张员工桌和一张假期表,其中每一行代表他们度假的一天。
我希望让所有休假时间更长的员工享受最长的休假日价值。
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)
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分: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)
答案 3 :(得分: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.*)