simplify case statement with function?

From: Rory Campbell-Lange <rory(at)campbell-lange(dot)net>
To: Postgresql Novice List <pgsql-novice(at)postgresql(dot)org>
Subject: simplify case statement with function?
Date: 2002-06-27 21:58:44
Message-ID: 20020627215844.GA5341@campbell-lange.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

I have a case statement that is quite long to write!

I'd be greteful to know if I can simplify this by using a function, and
if so how I would go about 1) defining it and 2) using it.

Thanks for any help.

select id, idtext, title,
case when n_msgs > 0 then n_msgs else 0 end as n_ms,
case
when
ceil((extract(epoch from current_timestamp) - extract(epoch from ts_modified))/ (60 *60)) < 2
then 1
when
ceil((extract(epoch from current_timestamp) - extract(epoch from ts_modified))/ (60 *60)) < 4
then 4
when
ceil((extract(epoch from current_timestamp) - extract(epoch from ts_modified))/ (60 *60)) < 8
then 8
when
ceil((extract(epoch from current_timestamp) - extract(epoch from ts_modified))/ (60 *60)) < 24
then 24
else
2000
end as hours

--
Rory Campbell-Lange
<rory(at)campbell-lange(dot)net>
<www.campbell-lange.net>

Browse pgsql-novice by date

  From Date Subject
Next Message Josh Berkus 2002-06-27 22:37:03 Re: Date Question
Previous Message cmasters 2002-06-27 20:18:24 Re: Help with creating function