Unix Technical Forum

Unexpected behavior with CASE statement

This is a discussion on Unexpected behavior with CASE statement within the Pgsql General forums, part of the PostgreSQL category; --> Suppose I have the following table named "metrics": metric_type | val ------------+----- 0 | 1 0 | 1 1 ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-09-2008, 11:03 PM
Jimmy Choi
 
Posts: n/a
Default Unexpected behavior with CASE statement

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


Confidentiality Notice. This message may contain information that is confidential or otherwise protected from disclosure.
If you are not the intended recipient, you are hereby notified that any use, disclosure, dissemination, distribution,
or copying of this message, or any attachments, is strictly prohibited. Ifyou have received this message in error,
please advise the sender by reply e-mail, and delete the message and any attachments. Thank you.




---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:48 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com