ORACLE SQL Group财务年度

时间:2019-12-23 11:02:27

标签: sql oracle

计算每个财政年度加入公司的人数。 4月1日至3月31日。

select
case when month(joinDate)>=7 THEN concat(year(joindate),year(joindate)+1)
     else concat (year(joinDate)-1,year(joindate))
     end as Financial_Year
from employees
group by Financial_Year

3 个答案:

答案 0 :(得分:0)

您可以使用ADD_MONTHSEXTRACT函数来实现相同的功能。

SELECT
    FINANCIAL_YEAR || '-' || FINANCIAL_YEAR + 1 AS FINANCIAL_YEAR,
    NUMBER_OF_EMPLOYEES_JOINED
FROM
    (
        SELECT
            EXTRACT(YEAR FROM ADD_MONTHS(JOINDATE, - 3)) FINANCIAL_YEAR,
            COUNT(1) NUMBER_OF_EMPLOYEES_JOINED
        FROM
            EMPLOYEES
        GROUP BY
            EXTRACT(YEAR FROM ADD_MONTHS(JOINDATE, - 3))
    );

干杯!

答案 1 :(得分:0)

select count(*),
case when month(joinDate)>=7 THEN concat(year(joindate),year(joindate)+1)
     else concat (year(joinDate)-1,year(joindate))
     end as Financial_Year
from employees
group by Financial_Year

使用简单的COUNT(*)聚合函数即可完成工作

答案 2 :(得分:0)

只需减去(或添加)6个月并累加:

select add_months(join_date, -6) as fiscal_year,
       count(*)
from employees
group by add_months(join_date, -6)
order by min(join_date);

如果您希望结果为“ 2019-2020”,则:

select extract(year from add_months(join_date, -6)) || '-' || extract(year from add_months(join_date, +6)) as fiscal_year,
       count(*)
from employees
group by extract(year from add_months(join_date, -6)) || '-' || extract(year from add_months(join_date, +6))
order by min(join_date);

如果您尝试在MySQL中执行此操作(语法建议):

select concat_ws('_',
                 year(join_date - interval 6 month),
                 year(join_date + interval 6 month
                ) as fiscal_year,
         count(*)
from employees
group by fiscal_year;