Question

I've tried to find an answer everywhere, but can't seem to figure it out.

I'm trying to add to a column of a row when certain terms are met, and then have it display the results. Like,

select itemName
case demand when 'high' then price + 2
            when 'low' then price - 1
            else price
from itemTable

Assuming the data was something like:

Item1 | high | 2

the output would be:

Item1 | 4

This is in Oracle SQL Developer, and I've tried using a select statement inside of the then, but a subquery seems a little over the top for something like this.

Was it helpful?

Solution

You just need a comma to separate the two column definitions, and to give a name to the computed column;

select 
    itemName,    
    case demand 
        when 'high' then price + 2
        when 'low' then price - 1
        else price
    end AS demand
from itemTable
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top