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)

そのため、vacations テーブルからその 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 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 に答える
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 に答える