/////
Search
Duplicate

Type of Triangles

태그
CASE
한 번 더 체크

문제

Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the following statements for each record in the table:
Equilateral: It's a triangle with  sides of equal length.
Isosceles: It's a triangle with  sides of equal length.
Scalene: It's a triangle with  sides of differing lengths.
Not A Triangle: The given values of AB, and C don't form a triangle.
Input Format
The TRIANGLES table is described as follows:
Each row in the table denotes the lengths of each of a triangle's three sides.

예시

Sample Input
Sample Output
Isosceles Equilateral Scalene Not A Triangle
Plain Text
복사

정답

SELECT (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 (B=C) OR (A=C) THEN 'Isosceles' ELSE 'Scalene' END) FROM TRIANGLES;
SQL
복사

풀이

CASE WHEN 의 사용법을 묻는 문제이다.
조건이 여러 개인 경우 다음과 같이 WHEN 절을 추가해서 사용 가능하다.
CASE WHEN expression1 THEN result1 WHEN expression2 THEN result2 ... ELSE resultn END
SQL
복사