P粉4470021272023-08-14 09:46:46
我假设你的"employee"表中有一个名为"start_date"的列,该列表示员工职位的开始日期。
SELECT AVG(DATEDIFF(MONTH, start_date, end_date)) AS average_time_in_position FROM ( SELECT e1.employee_id, e1.manager_id, e1.name, e1.start_date, COALESCE(e2.start_date, GETDATE()) AS end_date FROM employee e1 LEFT JOIN employee e2 ON e1.employee_id = e2.manager_id ) AS manager_positions;