当我分组时,Oracle错误不是单组组功能

时间:2017-03-13 12:04:10

标签: sql oracle

如果我通过以下方式包含该组,以下查询为什么会出现以下错误:

ORA-00937: not a single-group group function


   select petf.element_name en,
          --tm.i,
          --sum(xxpay_util.safe_to_number(prrv.result_value)) s
          nvl(max(decode(tm.i, 1, sum(xxpay_util.safe_to_number(prrv.result_value)), null)), 0) e1,
          nvl(max(decode(tm.i, 2, sum(xxpay_util.safe_to_number(prrv.result_value)), null)), 0) e2              
   from   (   
             select rownum i,
                    sub.*
             from   (
                       select trunc(decode(pbg.legislation_code, 'ZA', ptp.pay_advice_date, ptp.regular_payment_date), 'MM') month_date,
                              min(ptp.start_date) real_month_start,
                              max(ptp.end_date)   real_month_end
                       from   per_business_groups   pbg,
                              per_time_periods      ptp
                       where  pbg.business_group_id = :P_BG_ID
                       and    ptp.payroll_id = :PAYROLL_ID
                       and    decode(pbg.legislation_code, 'ZA', ptp.pay_advice_date, trunc(ptp.regular_payment_date, 'MM')) between xxpay_util.get_tax_year(pbg.legislation_code, :P_TAX_YEAR, 'start')
                                                                                                                                 and xxpay_util.get_tax_year(pbg.legislation_code, :P_TAX_YEAR, 'end')
                       group  by trunc(decode(pbg.legislation_code, 'ZA', ptp.pay_advice_date, ptp.regular_payment_date), 'MM')                                                                                                                                 
                       order  by 1
                    )  sub
          )  tm,  
          pay_element_classifications pec,
          pay_element_types_f         petf,
          pay_input_values_f          pivf,
          pay_run_result_values       prrv,
          pay_run_results             prr,
          pay_assignment_actions      paa,
          pay_payroll_actions         ppa,
          per_time_periods            ptp
   where  instr('|' || :PRIMARY_CLASS_IDS || '|', '|' || to_char(nvl(pec.parent_classification_id, -6969)) || '|') > 0
   and
   (
      petf.classification_id = pec.classification_id
      or instr('|' || :PRIMARY_CLASS_IDS || '|', '|' || to_char(nvl(petf.classification_id, -6969)) || '|') > 0
   )
   and    tm.real_month_end between petf.effective_start_date and petf.effective_end_date
   and    pivf.element_type_id = petf.element_type_id
   and    pivf.name = 'Pay Value'
   and    prrv.input_value_id = pivf.input_value_id
   and    nvl(prrv.result_value, '0') <> '0'
   and    prr.run_result_id = prrv.run_result_id   
   and    prr.status in ('P', 'PA')   -- RUN_RESULT_STATUS lookup
   and    prr.element_type_id = petf.element_type_id
   and    paa.assignment_action_id = prr.assignment_action_id
   and    paa.action_status = 'C'
   and    paa.assignment_id = :ASSIGNMENT_ID
   and    ppa.payroll_action_id = paa.payroll_action_id 
   and    ppa.action_status = 'C'
   and    ppa.action_type in ('B', 'F', 'G', 'I', 'O', 'Q', 'R', 'V', 'L')   -- = 'R'
   and    ptp.time_period_id = ppa.time_period_id
   and    ptp.start_date >= tm.real_month_start
   and    ptp.end_date   <= tm.real_month_end
   group  by petf.element_name
--        ,tm.i

:PRIMARY_CLASS_IDS是一个字符串,例如105|107|112|113,列出了主要的分类ID。

1 个答案:

答案 0 :(得分:0)

通过将整个查询放在另一个子查询中来修复它:

select sub2.element_name,
       max(decode(sub2.i, 11, sub2.result, null)) e11, 
       max(decode(sub2.i, 12, sub2.result, null)) e12
from   (
         select petf.element_name,   
                tm.i,
                sum(xxpay_util.safe_to_number(prrv.result_value)) result              
         from   (      
                   select rownum i,
                          sub.*
                   from   (
                             select trunc(decode(pbg.legislation_code, 'ZA', ptp.pay_advice_date, ptp.regular_payment_date), 'MM') month_date,
                                    min(ptp.start_date) real_month_start,
                                    max(ptp.end_date)   real_month_end
                             from   per_business_groups   pbg,
                                    per_time_periods      ptp
                             where  pbg.business_group_id = :P_BG_ID
                             and    ptp.payroll_id = :PAYROLL_ID
                             and    decode(pbg.legislation_code, 'ZA', ptp.pay_advice_date, trunc(ptp.regular_payment_date, 'MM')) between xxpay_util.get_tax_year(pbg.legislation_code, :P_TAX_YEAR, 'start')
                                                                                                                                       and xxpay_util.get_tax_year(pbg.legislation_code, :P_TAX_YEAR, 'end')
                             group  by trunc(decode(pbg.legislation_code, 'ZA', ptp.pay_advice_date, ptp.regular_payment_date), 'MM')                                                                                                                                 
                             order  by 1
                          )  sub                    
                )  tm,          
                (          
                   select distinct petf.element_type_id
                   from   pay_element_classifications pec,
                          pay_element_types_f         petf
                   where  instr('|' || :PRIMARY_CLASS_IDS || '|', '|' || to_char(nvl(pec.parent_classification_id, -6969)) || '|') > 0
                   and
                   (
                      petf.classification_id = pec.classification_id
                      or instr('|' || :PRIMARY_CLASS_IDS || '|', '|' || to_char(nvl(petf.classification_id, -6969)) || '|') > 0
                   )             
                )  ele,
                pay_element_types_f         petf,                                 
                pay_input_values_f          pivf,
                pay_run_result_values       prrv,
                pay_run_results             prr,
                pay_assignment_actions      paa,
                pay_payroll_actions         ppa
         where  petf.element_type_id = ele.element_type_id          
         and    tm.real_month_end between petf.effective_start_date and petf.effective_end_date
         and    pivf.element_type_id = petf.element_type_id
         and    pivf.name = 'Pay Value'
         and    prrv.input_value_id = pivf.input_value_id
         and    nvl(prrv.result_value, '0') <> '0'
         and    prr.run_result_id = prrv.run_result_id   
         and    prr.status in ('P', 'PA')   -- RUN_RESULT_STATUS lookup
         and    prr.element_type_id = petf.element_type_id
         and    paa.assignment_action_id = prr.assignment_action_id
         and    paa.action_status = 'C'
         and    paa.assignment_id = :ASSIGNMENT_ID
         and    ppa.payroll_action_id = paa.payroll_action_id 
         and    ppa.action_status = 'C'
         and    ppa.action_type in ('B', 'F', 'G', 'I', 'O', 'Q', 'R', 'V', 'L')   -- = 'R'
         and    ppa.effective_date between tm.real_month_start and tm.real_month_end
         and    petf.element_name in ('ZA_Tax_Costing', 'ZA_UIF_Employee_Contribution')
         group  by petf.element_name,   
                tm.i
       ) sub2 
group  by sub2.element_name