SELECT YEAR, period, round((1- sum(rej_qty) / sum(recd_qty))*100, 0) FROM TAB_A WHERE sid = '200' AND sdid IN ('4750') AND ( ( YEAR ='2011' AND period IN('01_JAN') ) OR ( YEAR = '2010' AND period IN('02_FEB','03_MAR','04_APR','05_MAY','06_JUN','07_JUL','08_AUG','09_SEP','10_OCT','11_NOV','12_DEC') ) ) group by year, period For a particular month, recd_qty is ZERO because of which I am getting DIVIDE BY ZERO error.Is there any way to avoid DIVIDE BY ZERO error?I there any way where in that particular month is ignored?
4 Réponses :
Si vous souhaitez ignorer de tels enregistrements, vous pouvez utiliser une sous-requête
SELECT YEAR, period, DECODE(recd_qty, 0, NULL, round((1- sum(rej_qty) / sum(recd_qty))*100, 0))
Le où code> clause ne résoudrait pas le problème car il sommait
recd_qty code> S avant de diviser.
@Tim merci, j'ai changé le SQL pour une sous-requête
@Kenny c'est la chose ... ça peut ne pas être précis :)
Il serait parfaitement légal pour Oracle d'évaluer l'expression dans la liste Select avant d'évaluer la clause où la clause WHERE, il n'ya donc aucune garantie que cela éliminerait l'erreur ou qu'une modification future du plan de requête ne ferait pas que l'erreur se reproduit. Vous auriez besoin d'un décodage / de cas même si vous utilisez une sous-requête-- Sélectionnez une année, période, (cas lorsque RECD_SUM = 0 puis NULL SINGE ROUND ((1 - REJ_SUM / RECD_SUM) * 100,0) FIN) < / code>
Avez-vous essayé d'utiliser Oracle DOC Un autre exemple de nullif () code>?
http://www.oracle-base.com/articles/9i/ansiisosqlsupport .php # nulliffunction p>
http: / /www.bennadel.com/blog/984-utilisant-nullif-to-prevent-divide-by-zero-errors-in-sql.htm P> P>
round(ISNULL( ((1- sum(rej_qty)) / NULLIF( (sum(recd_qty))*100), 0 )), 0 ),0) If you replace your division using NULLIF to set a NULL when there is divide by zero, then an ISNULL to replace the NULL with a 0 - or indeed whatever value you want it to.
cas lorsque la somme (RECD_QY) <> 0 puis rond ((1- somme (rej_qty) / somme (recd_qty)) * 100, 0) sinon 0 extrémité p>