【问题标题】:create a new column within a select statement在 select 语句中创建一个新列
【发布时间】:2015-04-07 20:35:16
【问题描述】:

这是我正在使用的代码。

 select address, area,
 case area
 when area < 1500 then 'small'
 when area > 2500 then 'large'
 else 'medium'
 end as size
 from listings
 where zip = 95677;

有了这个,我想创建一个新列,根据面积大小告诉你房子是大是小。

这是我得到的错误,我不确定如何转换它以使其工作。如果这就是提示所说的解决方案。

第 3 行:当面积

【问题讨论】:

  • case when area ... 而不是 case area when area ...

标签: sql postgresql casting case


【解决方案1】:

正确的语法是:

 select address, area,
        (case when area < 1500 then 'small'
              when area > 2500 then 'large'
              else 'medium'
         end) as size
 from listings
 where zip = 95677;

case 有两种形式。以上是case when &lt;condition&gt; 形式,其中when 子句是完全形成的条件。另一种形式更像是 C 中的 switch 语句:case area when 10 then 'ten' when 20 then 'twenty' . . .。此表单只能测试单个表达式的相等性。

【讨论】:

    猜你喜欢
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2021-01-28
    • 2012-11-04
    • 2012-12-28
    • 2019-06-18
    • 2017-02-22
    相关资源
    最近更新 更多