【问题标题】:SQL : select one column if two columns are equal, else select bothSQL:如果两列相等则选择一列,否则选择两者
【发布时间】:2016-06-29 11:49:33
【问题描述】:

我有以下 SQL 选择问题:

我有两列 positive thresholdnegative threshold(在其他几列中,例如名称、ids....)。

  • 如果它们的(绝对)值相同(乘以 -1),那么我只想选择正阈值作为列 [threshold]

  • 如果值不同,我想选择[positiveThreshold][negativeThreshold]两列。

提前谢谢你。

【问题讨论】:

  • 一条SQL语句返回一组固定的列,每一行需要有相同的列。请提供样本数据和期望的结果。

标签: sql select case


【解决方案1】:
select null as [threshold], positivethreshold, negativethreshold 
from table 
where negativethreshold is null 
or (positivethreshold + negativethreshold) <> 0 
union
select positivethreshold, null, null
from table 
where (positivethreshold + negativethreshold) = 0

【讨论】:

    猜你喜欢
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    • 2013-06-01
    • 1970-01-01
    相关资源
    最近更新 更多