ORACLE GROUP BY和CASE WHEN同时使用时遇到的一些问题

话不多说,上源码

select
  sum(case when chg.ar_ap = 0 and chg.currency = 1 then chg.amount else 0 end) profit_cny_ar,
  sum(case when chg.ar_ap = 1 and chg.currency = 1 then chg.amount else 0 end) profit_cny_ap,
  sum(case when chg.ar_ap = 0 and chg.currency = 2 then chg.amount else 0 end) profit_usd_ar,
  sum(case when chg.ar_ap = 1 and chg.currency = 2 then chg.amount else 0 end) profit_usd_ap,
  sum(case when chg.ar_ap = 0 and chg.currency = 3 then chg.amount else 0 end) profit_eur_ar,
  sum(case when chg.ar_ap = 1 and chg.currency = 3 then chg.amount else 0 end) profit_eur_ap,
  (case when jh.is_master = 1 then jh.job_id when rel.job_id is null then jh.job_id else rel.job_id end) job_id,
  jh.mbl_no  mbl_no
  from acc_charge chg
  left join job_head jh
    on chg.job_id = jh.job_id
  LEFT JOIN acc_bill bill
    on (chg.bill_id = bill.bill_id)
  LEFT JOIN job_head_rel rel
    on chg.job_id = rel.rel_job_id 
 where ((chg.ar_ap = 0 and                     
       (chg.bill_id in (SELECT distinct tk.bill_id          
                            FROM job_ticket_rel jtr, acc_bill tk
                           WHERE jtr.object_type = 14       
                             AND jtr.object_id = tk.bill_id
                             and tk.corp_head_id = 8        
                             and tk.status != 9) or        
       chg.confirm_id in
       (SELECT distinct tk.confirm_id               
             FROM job_ticket_rel jtr, acc_confirm tk
            WHERE jtr.object_type = 13               
              AND jtr.object_id = tk.confirm_id
              and tk.open_ticket_type != 1))) or        
       (chg.ar_ap = 1 and                      
       (chg.bill_id is null or exists
        (select 1
             from acc_bill bill
            where bill.bill_id = chg.bill_id
              and bill.corp_head_id = 8))))             
   and chg.charge_class_type != 4
   and exists (select 1
          from sys_staff_right_charge right_charge          
         where is_ar = 1                      
           and is_select = 1
           and right_charge.charge_type_id = chg.charge_type_id)
   group by job_id, jh.mbl_no;
   

这是我开发时候写的sql,结果group by的时候,显示job_id出错,然后将代码改为下面形式:

select max(a.profit_cny_ar), max(a.profit_cny_ap), 
       max(a.profit_usd_ar), max(a.profit_usd_ap), 
       max(a.profit_eur_ar), max(a.profit_eur_ap), 
       a.job_id, a.mbl_no
from
(select
  sum(case when chg.ar_ap = 0 and chg.currency = 1 then chg.amount else 0 end) profit_cny_ar,
  sum(case when chg.ar_ap = 1 and chg.currency = 1 then chg.amount else 0 end) profit_cny_ap,
  sum(case when chg.ar_ap = 0 and chg.currency = 2 then chg.amount else 0 end) profit_usd_ar,
  sum(case when chg.ar_ap = 1 and chg.currency = 2 then chg.amount else 0 end) profit_usd_ap,
  sum(case when chg.ar_ap = 0 and chg.currency = 3 then chg.amount else 0 end) profit_eur_ar,
  sum(case when chg.ar_ap = 1 and chg.currency = 3 then chg.amount else 0 end) profit_eur_ap,
  (case when jh.is_master = 1 then jh.job_id when rel.job_id is null then jh.job_id else rel.job_id end)job_id,
--  nvl(rel.job_id, jh.job_id) job_id,
  jh.mbl_no  mbl_no
  from acc_charge chg
  left join job_head jh
    on chg.job_id = jh.job_id
  LEFT JOIN acc_bill bill
    on (chg.bill_id = bill.bill_id)
  LEFT JOIN job_head_rel rel
    on chg.job_id = rel.rel_job_id 
 where ((chg.ar_ap = 0 and                     
       (chg.bill_id in (SELECT distinct tk.bill_id          
                            FROM job_ticket_rel jtr, acc_bill tk
                           WHERE jtr.object_type = 14        
                             AND jtr.object_id = tk.bill_id
                             and tk.corp_head_id = 8        -
                             and tk.status != 9) or        
       chg.confirm_id in
       (SELECT distinct tk.confirm_id                
             FROM job_ticket_rel jtr, acc_confirm tk
            WHERE jtr.object_type = 13                
              AND jtr.object_id = tk.confirm_id
              and tk.open_ticket_type != 1))) or          -
       (chg.ar_ap = 1 and                     
       (chg.bill_id is null or exists
        (select 1
             from acc_bill bill
            where bill.bill_id = chg.bill_id
              and bill.corp_head_id = 8))))             
   and chg.charge_class_type != 4
   and exists (select 1
          from sys_staff_right_charge right_charge        
         where is_ar = 1                      
           and is_select = 1
           and right_charge.charge_type_id = chg.charge_type_id)) a
   group by a.job_id, a.mbl_no;

结果还是报ORA-00937,NOT A SINGLE-GROUP GROUP FUNCTION 错误;

百般尝试之后还是没有好的解决办法,最后改成下面形式才不报错:

select
  sum(case when chg.ar_ap = 0 and chg.currency = 1 then chg.amount else 0 end) profit_cny_ar,
  sum(case when chg.ar_ap = 1 and chg.currency = 1 then chg.amount else 0 end) profit_cny_ap,
  sum(case when chg.ar_ap = 0 and chg.currency = 2 then chg.amount else 0 end) profit_usd_ar,
  sum(case when chg.ar_ap = 1 and chg.currency = 2 then chg.amount else 0 end) profit_usd_ap,
  sum(case when chg.ar_ap = 0 and chg.currency = 3 then chg.amount else 0 end) profit_eur_ar,
  sum(case when chg.ar_ap = 1 and chg.currency = 3 then chg.amount else 0 end) profit_eur_ap,
  (case when jh.is_master = 1 then jh.job_id when rel.job_id is null then jh.job_id else rel.job_id end) job_id,
  jh.mbl_no  mbl_no
  from acc_charge chg
  left join job_head jh
    on chg.job_id = jh.job_id
  LEFT JOIN acc_bill bill
    on (chg.bill_id = bill.bill_id)
  LEFT JOIN job_head_rel rel
    on chg.job_id = rel.rel_job_id 
 where ((chg.ar_ap = 0 and                     
       (chg.bill_id in (SELECT distinct tk.bill_id          
                            FROM job_ticket_rel jtr, acc_bill tk
                           WHERE jtr.object_type = 14       
                             AND jtr.object_id = tk.bill_id
                             and tk.corp_head_id = 8        
                             and tk.status != 9) or        
       chg.confirm_id in
       (SELECT distinct tk.confirm_id               
             FROM job_ticket_rel jtr, acc_confirm tk
            WHERE jtr.object_type = 13               
              AND jtr.object_id = tk.confirm_id
              and tk.open_ticket_type != 1))) or        
       (chg.ar_ap = 1 and                      
       (chg.bill_id is null or exists
        (select 1
             from acc_bill bill
            where bill.bill_id = chg.bill_id
              and bill.corp_head_id = 8))))             
   and chg.charge_class_type != 4
   and exists (select 1
          from sys_staff_right_charge right_charge          
         where is_ar = 1                      
           and is_select = 1
           and right_charge.charge_type_id = chg.charge_type_id)
   group by (case when jh.is_master = 1 then jh.job_id when rel.job_id is null then jh.job_id else rel.job_id end), jh.mbl_no;
   

将case语句当成一个字段,加在group by后面,解决此问题,但是感觉这种做法是饮鸩止渴,不清楚原理,慎用。

空了一定回来刨根问底,FLAG在此!

你可能感兴趣的:(工作总结,错误经验,ORACLE,GROUP,BY,CASE,WHEN)