1. 程式人生 > >Oracle按日期累計求和

Oracle按日期累計求和

  • 求各個部門在員工入職時(年份-月份)的該部門的總人數

    寫法1:

    SELECT DISTINCT HIRE_YEAR_MONTH, DEPARTMENT_ID,  COUNT_
    FROM (
    SELECT 
    DEPARTMENT_ID, 
    TO_CHAR(HIRE_DATE, 'yyyy/mm') HIRE_YEAR_MONTH, 
    COUNT(*) OVER(PARTITION BY DEPARTMENT_ID ORDER BY TO_CHAR(HIRE_DATE, 'yyyy/mm')) 
    AS COUNT_ 
    FROM HR.EMPLOYEES) 
    ORDER BY DEPARTMENT_ID, HIRE_YEAR_MONTH;

    寫法2:

    SELECT DEPARTMENT_ID,
       TO_CHAR(HIRE_DATE, 'yyyy/mm') HIRE_YEAR_MONTH,
       COUNT(*),
       SUM(COUNT(*)) OVER(PARTITION BY DEPARTMENT_ID ORDER BY TO_CHAR(HIRE_DATE, 'yyyy/mm')) AS COUNT_
    FROM HR.EMPLOYEES
    GROUP BY DEPARTMENT_ID, TO_CHAR(HIRE_DATE, 'yyyy/mm')