simplify case statement with function? - Mailing list pgsql-novice

From Rory Campbell-Lange
Subject simplify case statement with function?
Date
Msg-id 20020627215844.GA5341@campbell-lange.net
Whole thread Raw
List 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@campbell-lange.net>
<www.campbell-lange.net>



pgsql-novice by date:

Previous
From: cmasters
Date:
Subject: Re: Help with creating function
Next
From: Josh Berkus
Date:
Subject: Re: Date Question