ditto
ditto

Reputation: 6277

PostgreSQL: using where clause to change select value

I'm wanting to change a select value depending on the where clause. Is something like the following possible in any way?

SELECT b
FROM table
WHERE 
    IF date = '2016-03-24'
        b = 1
    ELSE IF date > date '2016-03-24' - 7 AND date < '2016-03-24'
        b = 2
    ELSE
        b = 1
ORDER BY date
LIMIT 1

Upvotes: 0

Views: 45

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520968

Use CASE WHEN:

SELECT CASE WHEN date = '2016-03-24' THEN 1
            WHEN date > '2016-03-17' AND date < '2016-03-24' THEN 2
            ELSE 1
       END AS b
FROM table
ORDER BY b
LIMIT 1

Upvotes: 1

Related Questions