9

I'm creating an SQL Query under oracle 10g, the result should give me something like that :

----------------------------------------------------------------------------------
TEXT         VALUE1        VALUE2          VALUE3               VALUE4
---------------------------------------------------------------------------------
TEXT1        8795           5684        value1-value2          value3/value2*100
TEXT2        235             568            ...                   ...
TEXT3        125             23             ...                   ...
TEXT4        789             58             ...                   ...
TEXTN         0               0             ...                   ...

when i try to calculate VALUE4 column i get this error :

ORA-01476: le diviseur est égal à zéro
01476. 00000 -  "divisor is equal to zero"
*Cause:    
*Action:

I tried DECODE function but i stil have the same error, it's the same for CASE

NB : VALUE1, VALUE2, VALUE3 and VALUE4 are calculated columns; VALUE1 = sum(col1)+sum(col2).. and so for other VALUE2 column.

Thanks and regards

4

2 回答 2

19

这完全取决于您是否要计算一个值是否会导致无限值。您可以忽略这些特定实例并按照Gordon 的回答所建议的那样计算余数:

case when value2 <> 0 then value3 / value2 * 100 end

或者,如果想忽略它们,您可以使用NULLIF()将值更改为 NULL 并且不计算任何内容:

value3 / nullif(value2, 0) * 100

我不理解您认为这是一个计算列会导致问题的论点。如果它是虚拟列,那么您的表将永远不会创建,如文档中所述,虚拟列不能通过名称引用另一个。

如果它不是虚拟列,那么您可以像往常一样在 select 语句中执行此操作。

于 2013-06-10T11:32:05.827 回答
5

在您的代码中,您拥有:

select value3/value2*100 as value4

你应该有:

select (case when value2 <> 0 then value3/value2*100 end) as value4

鉴于你的问题:

select value1, value2, value3,
       (case when value2 is not null then value3 / value2 * 100 end) as value4
from (select value1, value2, (value1 - value2) as value3
      from . . .
     )
于 2013-06-10T11:22:17.680 回答