在我的查询中除数等于零

时间:2013-07-20 13:58:31

标签: sql oracle10g

感谢您阅读我的问题。

我有以下查询。

   round(count(product)*100/(select sum(count(product)) FROM FACTORY 
   WHERE TRUNC(COMPLETED) >= '01-MAR-2013' and 
   TRUNC(COMPLETED) <= '31-MAR-2013' 
   group by (product))) as percentage FROM FACTORY

我收到以下错误。

    Error: ORA-01476: divisor is equal to zero
    SQLState:  22012
    ErrorCode: 1476

我试图把COALESCE但仍然得到同样的错误。

  round(count(product)*100/(select COALESCE(sum(count(product)),0) FROM FACTORY 
  WHERE TRUNC(COMPLETED) >= '01-MAR-2013' and 
  TRUNC(COMPLETED) <= '31-MAR-2013' 
  group by (product))) as percentage FROM FACTORY

请帮助。

2 个答案:

答案 0 :(得分:1)

尝试

1)

SELECT  xxx/ NULLIF(yyyy,0) FROM [dbo].[tablename]

2)

SELECT  COALESCE(xxx / NULLIF(yyyy,0), 0) FROM [dbo].[tablename]

答案 1 :(得分:0)

使用where子句过滤掉数据,这样就不会得到0值。我在where子句

中添加了产品!= 0