0

Let say we have such table:

id | bird      | color
1  | raven     | black
2  | gull      | white
3  | broadbill | green
4  | goldfinch | yellow 
5  | tit       | yellow 

Is it possible in PostgreSQL to write such SELECT expression, which can make dynamic alias for the color column? This aliase's name should depend on selected data from the color column. It is assumed, that only one row is returned (i.e., LIMIT 1 is applied at the end).

Pseudocode:

SELECT id, bird, color
 as 'bw' if color.value in ['black', 'white']
 else
 as 'colored'
FROM table
WHERE color = 'white'
LIMIT 1

Returning examples:

-- WHERE color = 'white'
id | bird | bw
1  | gull | white

-- WHERE color = 'yellow'
id | bird      | colored
4  | goldfinch | yellow
Volodymyr Nabok
  • 415
  • 1
  • 4
  • 11

0 Answers0