我试图显示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列?
我需要为报告生成器,因此我可以正确地表示该信息
答案 0 :(得分:0)
我希望您希望在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'
答案 1 :(得分:0)
我建议你把这些分组。我会这样做:
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;