每年有多少员工工作?

时间:2017-05-06 23:36:53

标签: sql oracle11g

我有简单的表员工ID,开始年份,结束年份。我必须找到有多少员工在最短的一年到最高年份工作。如果分钟是2000年和最大year sysdate它应列出2000年至2017年所有那些年份的工作人员数

1 个答案:

答案 0 :(得分:0)

我结合了一些问题,我现在想的就是我不会,请检查这是否是无问题的解决方案:

with YEARS as ( select (to_char(SAP_VALID_FROM,'YYYY') + level -1) AS YEARS from ( SELECT MIN(SAP_VALIDFROM) AS SAP_VALID_FROM, MAX(SYSDATE) AS SAP_VALID_TO FROM MA_MITARBEITER_TH ) YEARS connect by level < = to_char(SYSDATE,'YYYY') - to_char(SAP_VALID_FROM,'YYYY')+1 ), YEARS_SCOPE AS ( SELECT PERSONALNUMMER, EXTRACT(YEAR FROM (MIN(SAP_VALIDFROM))) AS ARBEIT_BEGINN, CASE WHEN EXTRACT(YEAR FROM (MAX(SAP_VALIDTO)))='9999' THEN EXTRACT(YEAR FROM (MAX(SYSDATE))) ELSE EXTRACT(YEAR FROM (MAX(SAP_VALIDTO))) END AS ARBEIT_SCHLUSS FROM MA_MITARBEITER_TH WHERE TIMEGROUP='AKT' GROUP BY PERSONALNUMMER ) select COUNT(*), YEARS FROM YEARS,YEARS_SCOPE WHERE YEARS BETWEEN ARBEIT_BEGINN AND ARBEIT_SCHLUSS GROUP BY YEARS ORDER BY YEARS;