百分比等于零

时间:2014-03-10 02:27:53

标签: sql oracle

请帮助我,我遇到了这个错误“ORA-01476:divisor等于零”而另一件事,当第1列(g.ticket)或第2列(h.employee)的值为0时相反,获得1比例0.如何解决?谢谢。我非常感谢你的帮助。

查询:

    select 'Data'
    ||','||to_char(d.dtime_day, 'MM/dd/yyyy')
    ||','||nvl(g.ticket, 0 )
    ||','||round((nvl(g.ticket,2) / nvl(h.employee,2)),2)
    ||','||nvl(h.employee,0)
    ||','||round((nvl(h.employee,2) / nvl(g.ticket,2)),2)
    from owner_dwh.dc_date d
    left join (
        select trunc(t.create_time,'IW') as ddate
              ,count(case when upper(ticket_customer_user_id) = upper('hcphuser') then 1   else null end) as ticket
        from app_account.otrs_ticket t 
        where t.create_time between to_date('02/20/2013','MM/dd/yyyy') and sysdate -1
        and queue_id in (4,6,8,9,10,12,13,14,15,16,20,27,28,30,31,32,35,36,37,38,39,40,44,45,46,47,50,52,53,55,56)
        group by trunc(t.create_time,'IW')
    ) g on d.dtime_day = g.ddate
    left join (
       select trunc(e.date_hired, 'IW') as hdate
             ,count(e.date_hired) as employee
       from app_account.hr_employee e
       where e.date_hired between to_date('02/20/2013','MM/dd/yyyy') and sysdate -1
       group by trunc(e.date_hired, 'IW')
   ) h on d.dtime_day = h.hdate
   where d.dtime_day between to_date('02/20/2013','MM/dd/yyyy') and trunc(sysdate) -1
   and trunc(d.dtime_day, 'IW')= d.dtime_day
   order by d.dtime_day;

1 个答案:

答案 0 :(得分:1)

很少但不清楚你真正想要发生什么,但你应该能够避免使用CASE表达式获得ORA-01476。请尝试这样的事情:

 select 'Data'
    ||','||to_char(d.dtime_day, 'MM/dd/yyyy')
    ||','||nvl(g.ticket, 0 )
    ||','||CASE WHEN nvl(h.employee,0) = 0 THEN 0 
                ELSE round((nvl(g.ticket,0) / h.employee),2) END
    ||','||nvl(h.employee,0)
    ||','||CASE WHEN nvl(g.ticket,0) = 0 THEN 0 
                ELSE round((nvl(h.employee,0) / g.ticket),2) END
    from owner_dwh.dc_date d
<REST OF STATEMENT>