2016-06-29 62 views
0

我有以下SQL选择问题:SQL:选择一列,如果两列相等,否则选择既

我有两列positive thresholdnegative threshold(如姓名其他几个栏目中,IDS ....) 。

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

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

在此先感谢您。

+5

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

回答

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