HAZEL

[SQL : HackerRank/Leetcode] CASE 문 본문

DATA ANALYSIS/SQL

[SQL : HackerRank/Leetcode] CASE 문

Rmsid01 2021. 2. 8. 04:39

1. Type of Triangle


>> 문제

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 A, B, and C don't form a triangle.

Input Format

The TRIANGLES table is described as follows:

Sample Input

Sample Output

Isosceles Equilateral Scalene Not A Triangle

 

 

>> 문제 푼 코드 

SELECT CASE
            WHEN A = B AND A = C AND B = C THEN 'Equilateral'
            WHEN A + B <= C OR A + C <= B OR B + C <= A THEN 'Not A Triangle'
            WHEN A = B OR A = C OR B = C THEN 'Isosceles'
            ELSE 'Scalene' 
            
    END AS TRI
FROM TRIANGLES

 

** 주의할 점.

WHEN 문을 사용할 때, 순서가 중요하다. 처음에 NOT A TRIANGLE 을 맨 밑에 썼더니, 에러가 계속 났다.. 알고보니, WHEN 순서를 잘못 썼기 때문이다. ㅠㅠ 

 

www.hackerrank.com/challenges/what-type-of-triangle/problem?h_r=internal-search

 

Type of Triangle | HackerRank

Query a triangle's type based on its side lengths.

www.hackerrank.com

 

2. 1179. Reformat Department Table


>> 문제

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| revenue       | int     |
| month         | varchar |
+---------------+---------+
(id, month) is the primary key of this table.
The table has information about the revenue of each department per month.
The month has values in ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"].

 

Write an SQL query to reformat the table such that there is a department id column and a revenue column for each month.

The query result format is in the following example:

Department table:
+------+---------+-------+
| id   | revenue | month |
+------+---------+-------+
| 1    | 8000    | Jan   |
| 2    | 9000    | Jan   |
| 3    | 10000   | Feb   |
| 1    | 7000    | Feb   |
| 1    | 6000    | Mar   |
+------+---------+-------+

Result table:
+------+-------------+-------------+-------------+-----+-------------+
| id   | Jan_Revenue | Feb_Revenue | Mar_Revenue | ... | Dec_Revenue |
+------+-------------+-------------+-------------+-----+-------------+
| 1    | 8000        | 7000        | 6000        | ... | null        |
| 2    | 9000        | null        | null        | ... | null        |
| 3    | null        | 10000       | null        | ... | null        |
+------+-------------+-------------+-------------+-----+-------------+

Note that the result table has 13 columns (1 for the department id + 12 for the months).

 

>> 문제푼 코드 

# Write your MySQL query statement below
SELECT ID , 
    SUM(CASE
        WHEN MONTH = 'Jan' THEN REVENUE ELSE NULL  
        END ) as 'Jan_Revenue',
    SUM(CASE
        WHEN MONTH = 'Feb' THEN REVENUE ELSE NULL  
        END ) as 'Feb_Revenue',
    SUM(CASE
        WHEN MONTH = 'Mar' THEN REVENUE ELSE NULL  
        END ) as 'Mar_Revenue',      
    SUM(CASE
        WHEN MONTH = 'Apr' THEN REVENUE ELSE NULL  
        END ) as 'Apr_Revenue',
    SUM(CASE
        WHEN MONTH = 'May' THEN REVENUE ELSE NULL  
        END ) as 'May_Revenue',
    SUM(CASE
        WHEN MONTH = 'Jun' THEN REVENUE ELSE NULL  
        END ) as 'Jun_Revenue',      
    SUM(CASE
        WHEN MONTH = 'Jul' THEN REVENUE ELSE NULL  
        END ) as 'Jul_Revenue',
    SUM(CASE
        WHEN MONTH = 'Aug' THEN REVENUE ELSE NULL  
        END ) as 'Aug_Revenue',
    SUM(CASE
        WHEN MONTH = 'Sep' THEN REVENUE ELSE NULL  
        END ) as 'Sep_Revenue',
    SUM(CASE
        WHEN MONTH = 'Oct' THEN REVENUE ELSE NULL  
        END ) as 'Oct_Revenue',
    SUM(CASE
        WHEN MONTH = 'Nov' THEN REVENUE ELSE NULL  
        END ) as 'Nov_Revenue',
    SUM(CASE
        WHEN MONTH = 'Dec' THEN REVENUE ELSE NULL  
        END ) as 'Dec_Revenue'
        
FROM DEPARTMENT
GROUP BY ID

** comment

: 컬럼을 13개 만들어줘야해서, 하나하나 sum 을 써주었다... ( 파이썬의 for문이 그리웠다... ) 

 

leetcode.com/problems/reformat-department-table/