일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
- sigmoid
- NLP
- GRU
- 카이제곱분포
- LSTM
- 자연어 논문
- nlp논문
- t분포
- Window Function
- torch
- 표준편차
- 논문리뷰
- SQL코테
- 설명의무
- airflow
- Statistics
- 코딩테스트
- CASE
- HackerRank
- MySQL
- update
- 짝수
- sql
- inner join
- 그룹바이
- 자연어 논문 리뷰
- 자연어처리
- SQL 날짜 데이터
- 서브쿼리
- leetcode
- Today
- Total
HAZEL
[SQL : HackerRank/Leetcode] CASE 문 본문
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
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/
'DATA ANALYSIS > SQL' 카테고리의 다른 글
[SQL : HackerRank] UNION (0) | 2021.02.14 |
---|---|
[SQL : HackerRank/Leetcode] SELF JOIN (0) | 2021.02.13 |
[SQL : Leetcode] LEFT JOIN 문 (0) | 2021.02.13 |
[SQL : HackerRank/Leetcode] INNER JOIN (0) | 2021.02.12 |
[SQL : GROUP BY/ where 서브쿼리 ] HackerRank: Top Earners (0) | 2021.02.12 |