繁体   English   中英

在oracle中连续比较多个列

[英]compare multiple columns in a row in oracle

我有一个来自HackerRank的问题:样本输入:

select * from triangle;
A   B   C
20  20  23
20  20  20
20  21  22
13  14  30

样本输出应为:

Isosceles
Equilateral
Scalene
Not a triangle (as sum of two sides not greater than the third side)

我已经用Case进行过尝试,但是由于在其中添加了很多条件,因此变得混乱

select 
case
when ((A+B) > C ) or ((B+C)>A) or ((C+A)>B)then  
 case when (A=B) and (B=C) then 'equilateral'
 when (A=B ) or (B=C) or (C=A) then 'isosceles'
when (A!=B) and (B!=C) and (C!=A) then 'scalene'

end
else 'not triangle'
end Name from triangle;

我知道解码,但是解码在这里不起作用。 有没有比使用CASE更好的方式可以构建此代码?

select a, b, c,
       case when a + b < c or a + c < b or b + c < a then 'Not a triangle'
            when a = b and b = c then 'Equilateral'
            when a = b or a = c or b = c then 'Isosceles'
            else 'Scalene'
       end description
  from triangle

输出:

         A          B          C DESCRIPTION
---------- ---------- ---------- --------------
        20         20         23 Isosceles
        20         20         20 Equilateral
        20         21         22 Scalene
        13         14         30 Not a triangle

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM