Re: CASE WHEN examples

From: "moribund" <dwalker(at)iximd(dot)com>
To: pgsql-questions(at)postgresql(dot)org
Subject: Re: CASE WHEN examples
Date: 1999-11-02 01:04:40
Message-ID: IMqT3.32$K5.15392@typhoon2.gnilink.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Here's a sample table...

create table t1(v1 int, v2 int);

And some sample data...

insert into t1 values(1, 2);
insert into t1 values(2,1);
insert into t1 values(2,100);
insert into t1 values(1,1);

And finally, an example of case when: we want to build a query which
returns the maximum of field v1 and v2 for each row. If the values are
equal, we will just return field v1.

select
case
when v1 > v2 then v1
when v2 > v1 then v2
else v1
end as "themax"
from t1;

It should return...

themax
------
2
2
100
1
(3 rows)

--
---
Damond Walker

Rich Ackerman <richmond(at)no(dot)shadow(dot)spam(dot)net> wrote in message
news:3819a23a(dot)66190466(at)news(dot)mia(dot)bellsouth(dot)net(dot)(dot)(dot)
> Does anyone have any examples of how to use the CASE WHEN? I've been
> looking around and haven't found anything beyond what is in Chap 5 of
> the User's Guide. Thanks for any tips.
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message zris 1999-11-02 01:25:36 Arrays
Previous Message soundar rajan 1999-11-01 23:33:09 unsubscribe