比较oracle中连续的多个列

时间:2017-06-05 10:14:55

标签: sql 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?

1 个答案:

答案 0 :(得分:5)

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