使用 SQL 显示特定时期的平均小时数

Show average hours in specific periods using SQL

我正在尝试显示 3 个时段的平均休假天数和病假天数。

到目前为止我有这个 SQL 查询:

SELECT AVG(VACATIONHOURS) AS 'Vacation hours', AVG(SICKLEAVEHOURS) AS 'SICK DAYS'
FROM HUMANRESOURCES.EMPLOYEE E
WHERE E.BIRTHDATE BETWEEN '01-01-1960' AND '12-31-1969'
UNION ALL
SELECT AVG(VACATIONHOURS) AS 'VACATION', AVG(SICKLEAVEHOURS) AS 'SICK DAYS'
FROM HUMANRESOURCES.EMPLOYEE E
WHERE E.BIRTHDATE BETWEEN '01-01-1970' AND '12-31-1979'
UNION ALL
SELECT AVG(VACATIONHOURS) AS 'VACATION', AVG(SICKLEAVEHOURS) AS 'SICK DAYS'
FROM HUMANRESOURCES.EMPLOYEE E
WHERE E.BIRTHDATE BETWEEN '01-01-1980' AND '12-31-1989'
;

我的输出只有 2 列病假和假期和 3 行数字。

我的问题是如何添加显示年份的第 3 列?

我需要它来生成报表,这样我就可以正确地表示该信息

我希望您希望在 where 子句中添加年份然后添加 max(years)

SELECT AVG(VACATIONHOURS) AS 'Vacation hours', 
       AVG(SICKLEAVEHOURS) AS 'SICK DAYS', 
       max('1960 to 1969')
 FROM HUMANRESOURCES.EMPLOYEE E
WHERE E.BIRTHDATE BETWEEN '01-01-1960' AND '12-31-1969'

我建议您将它们分组。我会这样做:

SELECT CONCAT(YEAR(BIRTHDATE), 's') as decade,
       AVG(VACATIONHOURS) AS `Vacation hours`,
       AVG(SICKLEAVEHOURS) AS `SICK DAYS`
FROM HUMANRESOURCES.EMPLOYEE E
GROUP BY decade
ORDER BY decade;