【问题标题】:If statement inside select clause (postgresql)选择子句中的 if 语句(postgresql)
【发布时间】:2018-05-11 18:30:14
【问题描述】:

我想根据列值设置 0 或 1 标志。如果值 > 0.0 AND

              Score         flag_score

              0.083642      1
              0.009542      1
              0.999999      1
              101.0000      0

【问题讨论】:

  • select score Score, case when score > 0.0 AND score <= 99.0 then 1 else 0 end flag_score from table;

标签: sql postgresql greenplum


【解决方案1】:

你试过case表达式吗?

select *, 
        (case when value > 0.0 and value <= 99.0 then 1 else 0 end) flag_score
from table;

【讨论】:

  • 案例陈述有效..但我们不能为上述案例编写 if 陈述
  • @user8545255 没有。SQL 标准中没有if 语句。
  • @user8545255:你可以编写自己的 if() 函数,如果它能让你开心的话。
【解决方案2】:

我会简单地写成:

select t.*, ( (value > 0.0) and (value <= 99.0) )::int as flag_score
from t;

Postgres 有一个很好的声明标志的简写,所以不需要case 表达式。

如果你想实际设置值,你可以这样做:

update t
    set flag = ( (value > 0.0) and (value <= 99.0) )::int;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 2013-03-14
    • 1970-01-01
    • 2018-08-27
    • 2010-09-21
    • 2015-07-25
    • 2013-03-02
    相关资源
    最近更新 更多