Search Postgresql Archives

Unexpected behavior with CASE statement

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Suppose I have the following table named "metrics":

metric_type | val
------------+-----
 0          | 1
 0          | 1
 1          | 0
 1          | 3

Now suppose I run the following simple query:

select
   metric_type,
   case metric_type
      when 0 then
         sum (1 / val)
      when 1 then
         sum (val)
   end as result
from metrics
group by metric_type

I expect to get the following result set:

metric_type | result
------------+-------
    0       |   2
    1       |   3

But in reality I get the following error:

    ERROR: division by zero
    SQL state: 22012

So it appears that Postgres executes all cases and select the result
in the end. Is this expected behavior?

Thanks
- Jimmy

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
       subscribe-nomail command to majordomo@xxxxxxxxxxxxxx so that your
       message can get through to the mailing list cleanly

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux