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;