요약통계를 출력함
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
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
리트코드 문제풀이
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
[멋사 SQL 3day] AARRR, 매출분석, 고객세분화분석 (1) | 2022.11.23 |
---|---|
[멋사 SQL 3day] Join, Union/Union all (0) | 2022.11.22 |
[멋사 SQL 2day] RFM Segmentation, 테이블 피봇 (0) | 2022.10.04 |
[멋사 SQL 1day] select, where, order by, 연산자 (0) | 2022.09.29 |