SQL/멋쟁이사자7기&데이터리안
[멋사 SQL 2day] 집계함수, group by, having, case when 조건문
dundunee
2022. 10. 4. 21:25
# 집계함수
요약통계를 출력함
- Count(*)
- Count(column): null값 빼고 집계
- Count(distinct column): 중복값, null값 제외
- sum(숫자 데이터를 가진 column)
- avg(숫자 데이터를 가진 column)
- 만약 column에 null 이 있다면 avg()와 sum()/count(*)의 값이 달라짐, 이럴경우 null값은 0으로 처리됨
- min()
- max()
# Group by, Having
- Group by
- 집계함수가 select절에 쓰인다면 집계함수가 쓰이지 않는 컬럼은 반드시 group by에 있어야 한다.
- 컬럼명 대신 select절에 작성한 번호(순서)를 써줘도 된다
- having
- select절에 들어간 집계함수에 대한 조건을 걸어줄 때 사용한다.
- where절과 같은 역할이나 적용되는 값이 다름을 알아야 한다.
# 해커랭크 문제풀이
- Revising Aggregations - Averages
select avg(population)
from CITY
where district = "California"
2. Revising Aggregations - The Sum Function
select sum(population)
from city
where district = 'California'
3. Revising Aggregations - The Count Function
#rounded down to the nearest integer.
select round(avg(population),0)
from city
4. Average Population
#rounded down to the nearest integer.
select round(avg(population),0)
from city
#rounded down to the nearest integer.
select floor(avg(population))
from city
5. Population Density Difference
select max(population) - min(population)
from city
6. Weather Observation Station 4
select count(city) - count(distinct city) #중복된 city의 개수
from station
7. Top Earners
# 서브쿼리 사용
select months * salary,
count(distinct employee_id)
from employee
where months * salary = (select max(months * salary) from Employee)
group by 1
select months * salary,
count(*)
from employee
group by months * salary
order by 1 desc
limit 1
# CASE ~ WHEN ~ THEN ~ ELSE ~ END AS ~
- select 절에 쓰이는 조건문이다. 파이썬의 if문과 똑같다.
- "집계함수(case ~ when ~ then ~ else ~ end) as ~ " 도 가능하다.
- 예를 들어 a 가구를 주문한 고객의 수는 몇명인가 등을 추출할 수 있다
# 해커랭크 문제풀이
9. Type of Triangle
select case
when a = b 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 b = c or a = c then "Isosceles"
else "Scalene"
end
from TRIANGLES
- case when문의 조건 순서가 중요함을 보여줌
- 우선 세 변이 모두 같으변 정사각형, 나머지 삼각형을 가르기 전에 삼각형이 되는 기본 조건( a + b ≤ c)를 판별해야함, 그러고 나면 삼각형이 될 수 있는 것만 남게되고, 거기서 이등변인지 그냥 삼각형인지 가르게 되는 것!
리트코드 문제풀이
1179. Reformat Department Table: 테이블 재정렬
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