Skip site navigation (1) Skip section navigation (2)

Conditional expresion in query

From: JORGE MALDONADO <jorgemal1960(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Conditional expresion in query
Date: 2012-02-09 15:25:08
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-novice
I have been reading about contidional expresions in a query and I have a
question about the following example included in the documentation:

       CASE WHEN a=1 THEN 'one'
            WHEN a=2 THEN 'two'
            ELSE 'other'
    FROM test;

I would like to make a few changes but I need to give a name (an alias) to
the result of  the CASE. Can an "AS" be added as shown below?

SELECT a, b,
       CASE WHEN a=b THEN a
            ELSE a || '-' || b
       END AS ab
    FROM test;

Jorge Maldonado


pgsql-novice by date

Next:From: Carlos MennensDate: 2012-02-09 15:33:11
Subject: Re: Two Tables That Share Data?
Previous:From: Philip CoulingDate: 2012-02-08 21:06:08
Subject: Re: Two Tables That Share Data?

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group