简体   繁体   English

添加到案例表达的列?

[英]adding to column in case expression?

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. 这是在Oracle SQL Developer中进行的,我尝试在then内使用select语句,但是对于类似这样的子查询似乎有点过头。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM