当前位置: 代码迷 >> Sql Server >> []怎么 取两列中 每行最小的值
  详细解决方案

[]怎么 取两列中 每行最小的值

热度:922   发布时间:2016-04-24 10:12:20.0
[求助]如何 取两列中 每行最小的值 ?
1   1  3  
2    10  3                    --3
3    10  10                --10
4    10  12               --10
5    10  9                 --9
6    10  11             ---10
7    10  17  
8    10  10  
9    11  11  
10    11  12  
11    12  12  
12    12  12  
13    13  4  
14    13  14  
15    13  12  
16    15  15  
17    15  16  
18   15  4
  
如上图 我 用如下sql 从2个表中查询出 如上数据
select a.bj,b.bj from tablea a,tableb b where a.xm=b.name;

现在求一个sql 能显示 第三列 其中第三列 为 前两列的 每行的最小数值。如果一样大 取任意一个。

------解决方案--------------------
select a.bj,b.bj ,case when (a.bj>=b.bj) then b.bj else a.bj end as  minbin from tablea a,tableb b where a.xm=b.name;
------解决方案--------------------

select a.bj,b.bj,case when a.bj>=b.bj then a.bj else b.bj end
from tablea a,tableb b where a.xm=b.name;

------解决方案--------------------

select a.bj,
b.bj,
case when a.bj>b.bj then b.bj
when a.bj<b.bj then a.bj
else a.bj
end
from tablea a,tableb b 
where a.xm=b.name;
  相关解决方案