用于将三角形分类为等边,等腰等的SQL查询

时间:2017-02-24 18:50:49

标签: sql oracle

请告诉我这段代码有什么问题? 我越来越 “第2行的错误: ORA-00907:缺少右括号“以下代码片段。请帮助我。

select (
IF (A = B or B = C or C = A) THEN
DBMS_OUTPUT.PUT_LINE('Isosceles')
ELSIF (A=B and B=c) THEN
DBMS_OUTPUT.PUT_LINE('Equilateral')
ELSIF (A != B) and (B != C) and (A+B>C or B+C>A or A+C>B)THEN
DBMS_OUTPUT.PUT_LINE('Scalene')
ELSE
DBMS_OUTPUT.PUT_LINE('Not A Triangle')
END IF
)
from triangles;

2 个答案:

答案 0 :(得分:2)

请记住CASE语句是按OR ORDER评估的 - 第一个符合"胜利的WHEN子句"并且没有进行进一步的评估。所以:

with
     triangles ( A, B, C ) as (
       select 20, 20, 23 from dual union all
       select 20, 20, 20 from dual union all
       select 20, 21, 22 from dual union all
       select 13, 14, 30 from dual
     )
select A, B, C, 
       case
            when A+B <= C  or B+C <= A or C+A <= B then 'Not A Triangle'                
            when A    = B and B    = C             then 'Equilateral'
            when A    = B  or B    = C or A    = C then 'Isosceles'
            else                                        'Scalene'  
            end  as classification
from   triangles
;

  A   B   C   CLASSIFICATION
--- --- ---   --------------
 20  20  23   Isosceles
 20  20  20   Equilateral
 20  21  22   Scalene
 13  14  30   Not A Triangle

4 rows selected.

答案 1 :(得分:0)

此代码适用于称为三角形类型的HackerRank SQL挑战,只需从SELECT子句中删除A,B,C:

select 
      case
          when A+B <= C  or B+C <= A or C+A <= B then 'Not A Triangle'                
          when A    = B and B    = C             then 'Equilateral'
          when A    = B  or B    = C or A    = C then 'Isosceles'
          else                                        'Scalene'  
          end  as classification
from   triangles;