Raphael Telatim Raphael Telatim - 5 months ago 8
SQL Question

Fill a column in SQL

Is it possible, on a single select, to fill a extra column on my select if the result satisfies a condition?

For example:

I have a table with 4 columns.

On a select, I want to, if hold_time is greater than X fill another column with another value:

dac | name | call_duration | hold_time
-------------------+-------+---------------+-----------
Desenvolvimento | 40018 | 1 | 13
Desenvolvimento | 40018 | 25 | 5
Desenvolvimento | 40018 | 2 | 6


If hold_time > 10 fill another column (that don't exist) with 'OK'.

Answer

You can do this with a case expression:

SELECT *, CASE WHEN hold_time > 10 THEN 'OK' END AS new_column
FROM   mytable