【问题标题】:Oracle sql window functions - total on same row as resultsOracle sql 窗口函数 - 总计与结果在同一行
【发布时间】:2014-10-09 17:37:55
【问题描述】:

我想使用窗口函数在同一行显示总(劳动力流动)和结果,但我无法让它工作。它在 total_labour_turnover 列中的 over 子句中为我提供了“未在预期位置找到关键字”。

基本上,我希望总劳动力流动率与各行的劳动力流动率一起显示。请注意,我不想要运行总计。

有人知道怎么做吗?

select active.cost_centre,
       active.flexi_perm,
       active.active_count,
       nvl(term.term_count, 0) term_count,
       round(nvl(term.term_count, 0) / active.active_count * 100, 2) labour_turnover,
       round(sum(nvl(term.term_count, 0)) / sum(active.active_count) * 100, 2) over (order by 1) total_labour_turnover
from   (       
          select haou.name cost_centre,
                 decode(payr.attribute2, 'F', 'Flexi', 'Perm') flexi_perm,
                 count(paaf.assignment_id) active_count
          from   per_periods_of_service      ppos,
                 per_all_assignments_f       paaf,
                 hr_organization_information hoi,
                 hr_all_organization_units   haou,
                 pay_all_payrolls_f          payr
          where  trunc(:active_count_date) between ppos.date_start and nvl(ppos.actual_termination_date, to_date('31/12/4712', 'dd/mm/yyyy'))
          and    paaf.period_of_service_id = ppos.period_of_service_id
          and    paaf.primary_flag = 'Y'
          and    trunc(:active_count_date) between paaf.effective_start_date and paaf.effective_end_date
          and    hoi.org_information_context = 'TRU_ADD_ORG'
          and    hoi.organization_id = paaf.organization_id
          and    haou.organization_id = paaf.organization_id
          and    payr.payroll_id = paaf.payroll_id
          and    payr.attribute2 in ('F', 'N')  -- Flexi and Non-Flexi
          and    trunc(:active_count_date) between payr.effective_start_date and payr.effective_end_date          
          group  by haou.name,
                 decode(payr.attribute2, 'F', 'Flexi', 'Perm')
       )  active,
       (              
          select haou.name cost_centre,
                 decode(payr.attribute2, 'F', 'Flexi', 'Perm') flexi_perm,
                 count(distinct paaf.person_id) term_count
          from   per_periods_of_service ppos,
                 per_all_assignments_f       paaf,
                 hr_organization_information hoi,
                 hr_all_organization_units   haou,
                 pay_all_payrolls_f          payr          
          where  nvl(ppos.actual_termination_date, to_date('31/12/4712', 'dd/mm/yyyy')) between trunc(:term_start) and trunc(:term_end)
          and    paaf.period_of_service_id = ppos.period_of_service_id
          and    paaf.primary_flag = 'Y'
          and    nvl(ppos.actual_termination_date, to_date('31/12/4712', 'dd/mm/yyyy')) between paaf.effective_start_date and paaf.effective_end_date
          and    hoi.org_information_context = 'TRU_ADD_ORG'
          and    hoi.organization_id = paaf.organization_id
          and    haou.organization_id = paaf.organization_id
          and    payr.payroll_id = paaf.payroll_id
          and    payr.attribute2 in ('F', 'N')  -- Flexi and Non-Flexi
          and    nvl(ppos.actual_termination_date, to_date('31/12/4712', 'dd/mm/yyyy')) between payr.effective_start_date and payr.effective_end_date          
          group  by haou.name,
                 decode(payr.attribute2, 'F', 'Flexi', 'Perm')                 
       )  term
where  term.cost_centre (+) = active.cost_centre
and    term.flexi_perm (+) = active.flexi_perm

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    请尝试以下方法,它应该会有所帮助:

    round(sum(nvl(term.term_count, 0)) over() / sum(active.active_count) over() * 100, 2) total_labour_turnover
    

    您的表达式中有两个 SUM 函数,它们都需要是分析函数。

    【讨论】:

      【解决方案2】:

      根据有关分析函数的 Oracle 文档,order by 子句自动暗示了 rows between unbounded preceding and current row 的运行窗口。

      如果您不想要一个总和,而是一个总和,请从您的分析 sum() 函数的 over 子句中删除 order by 1 并在其中放置一个 partition by null。或者用空括号试试,也可以。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 2014-04-09
        • 2014-05-02
        • 1970-01-01
        • 2016-11-06
        • 2017-08-06
        • 1970-01-01
        相关资源
        最近更新 更多