Type of Triangle in MYSQL

前端 未结 18 2157
执念已碎
执念已碎 2020-12-09 11:48

Problem statement:

Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the followin

相关标签:
18条回答
  • 2020-12-09 12:29
    SELECT
    CASE
        WHEN (A > 0 AND B > 0 AND C > 0 AND (A + B > C) AND (B + C > A) AND (A + C > B)) 
        THEN
        (
            CASE
                WHEN (A = B AND B = C) THEN 'Equilateral'
                WHEN (A = B OR B = C OR A = C) THEN 'Isosceles'
                ELSE 'Scalene'
            END
        )
        ELSE 'Not A Triangle'
    END
    FROM TRIANGLES;
    
    0 讨论(0)
  • 2020-12-09 12:31
    SELECT
    CASE
        WHEN (A+B>C AND B+C>A AND A+C>B) THEN
        (CASE
         WHEN A=B AND B=C AND A=C THEN 'Equilateral'
         WHEN A=B OR B=C OR A=C THEN 'Isosceles'
         ELSE 'Scalene'
         END)
        ELSE 'Not A Triangle'
    END
    FROM TRIANGLES;
    
    0 讨论(0)
  • 2020-12-09 12:31

    this answer is working well in sql * plus but not in hackerrank i dont know why? try this.

    declare
     cursor mytriangle is select A,B,C from triangles;
     a triangles.A %type;
     b triangles.B %type;
     c triangles.C %type;
     begin
     open mytriangle;
     if mytriangle%isopen then
     loop
     fetch mytriangle into a,b,c;
     exit when mytriangle %notfound;
     case
     when a+b<=c or b+c<=a or a+c<=b then
     dbms_output.put_line('Not A Triangle');
     when a=b and b=c then
     dbms_output.put_line('Equilateral');
     when a=b or b=c or c=a then
     dbms_output.put_line('Isoceles');
     when a+b>=c and b+c>=a and a+c>=b and a!=b and b!=c and c!=a then
     dbms_output.put_line('Scalene');
     else
     dbms_output.put_line('');
     end case;
     end loop;
     close mytriangle;
     else dbms_output.put_line('cannot open the cursor');
     end if;
    end;
    
    0 讨论(0)
  • 2020-12-09 12:32
    SELECT  CASE
        WHEN A+B>C AND B+C>A AND A+C>B THEN
            CASE 
                WHEN A=B AND B=C THEN 'Equilateral'
                WHEN A=B or A=C OR B=C THEN 'Isosceles'
                ELSE 'Scalene'
            END
        ELSE 'Not A Triangle'
    END
    FROM TRIANGLES 
    
    • Step 1: First we select case to check if given input is a triangle or not

    • Step 2: If it is a triangle then check for its type. So first case if all sides are the same, then the triangle type is 'Equilateral', then second case if any two sides are the same then the triangle type is 'Isosceles', otherwise the triangle type is 'Scalene'.

    0 讨论(0)
  • 2020-12-09 12:32

    SELECT IF(A+B>C AND A+C>B AND B+C>A, IF(A=B AND B=C, 'Equilateral', IF(A=B OR B=C OR A=C, 'Isosceles', 'Scalene')), 'Not A Triangle') FROM TRIANGLES;

    0 讨论(0)
  • 2020-12-09 12:35
       select case
    when (A+B<=C or B+C<=A or A+C<=B) then 'Not A Triangle'
    when (A=B and B=c) then 'Equilateral'
    when (A=B AND C<>B)or(B=C AND C<>A)or(A=C AND A<>B) then 'Isosceles'
    else 'Scalene'
    end as triangle_type
    from TRIANGLES;
    

    here, it will strictly check for the type of the triangles.

    0 讨论(0)
提交回复
热议问题