Ming's Life

[내일배움캠프] 2일차 본문

내일배움캠프

[내일배움캠프] 2일차

chamiii 2025. 5. 13. 20:07

1. 오늘 학습 키워드

  • SQL로 엑셀에서 자주 사용하는 형태로 데이터를 만든다,
  • 업무에 활용 할 수 있는 다양한 SQL 심화 문법을 익힌다.

 

2. 오늘 학습 한 내용을 나만의 언어로 정리하기

 

  • Null

- 사용할 수 없는 값일 때 해당 값을 연산에서 제외해준다. ( 0으로 간주 )

select restaurant_name,
       avg(rating) average_of_rating,
       avg(if(rating<>'Not given', rating, null)) average_of_rating2
from food_orders
group by 1

 

- where문 이용하여 null값 제거 한다. ( join시에는 innor join과 동일함 )

select a.order_id,
       a.customer_id,
       a.restaurant_name,
       a.price,
       b.name,
       b.age,
       b.gender
from food_orders a left join customers b on a.customer_id=b.customer_id
where b.customer_id is not null

 

- null 값일 때 : coalesce(age, 대체값)

select a.order_id,
       a.customer_id,
       a.restaurant_name,
       a.price,
       b.name,
       b.age,
       coalesce(b.age, 20) "null 제거",
       b.gender
from food_orders a left join customers b on a.customer_id=b.customer_id
where b.age is null

 

  • pivot table : 2개 이상의 기준으로 데이터를 집계할 때, 보기 쉽게 배열하여 보여 주는 것
select restaurant_name,
       max(if(hh='15', cnt_order, 0)) "15",
       max(if(hh='16', cnt_order, 0)) "16",
       max(if(hh='17', cnt_order, 0)) "17",
       max(if(hh='18', cnt_order, 0)) "18",
       max(if(hh='19', cnt_order, 0)) "19",
       max(if(hh='20', cnt_order, 0)) "20"
from 
(
select a.restaurant_name,
       substring(b.time, 1, 2) hh,
       count(1) cnt_order
from food_orders a inner join payments b on a.order_id=b.order_id
where substring(b.time, 1, 2) between 15 and 20
group by 1, 2
) a
group by 1
order by 7 desc

 

* 앞에 Max !

 

  • Window Function 의 기본 구조
window_function(argument) over (partition by 그룹 기준 컬럼 order by 정렬 기준)

 

  • Rank : 특정 기준으로 순위를 매겨주는 기능
select cuisine_type,
       restaurant_name,
       order_count,
       rn "순위"
from
(
select cuisine_type,
       restaurant_name,
       rank() over (partition by cuisine_type order by order_count desc) rn,
       order_count
from
(
select cuisine_type, restaurant_name, count(1) order_count
from food_orders
group by 1, 2
) a
) b
where rn<=3
order by 1, 4

 

  • Sum : 합계 ( 다만, 누적합이 필요하거나 카테고리별 합계컬럼와 원본 컬럼을 함께 이용할 때 유용하게 사용 )
select cuisine_type,
       restaurant_name,
       cnt_order,
       sum(cnt_order) over (partition by cuisine_type) sum_cuisine,
       sum(cnt_order) over (partition by cuisine_type order by cnt_order) cum_cuisine
from
(
select cuisine_type, 
	restaurant_name, 
	count(1) cnt_order
from food_orders
group by 1, 2
) a
order by cuisine_type , cnt_order

 

* 누적합 : order by

 

  • 날짜 포맷 date type
select date(date) date_type,
       date_format(date(date), '%Y') "년",
       date_format(date(date), '%m') "월",
       date_format(date(date), '%d') "일",
       date_format(date(date), '%w') "요일"
from payments

* 요일은 0(일요일) - 6(토요일) 순

 

 

 

3. 학습하며 겪었던 문제점 & 에러 

 

이해가 어렵고 익숙치않아 강의 복습

 

4. 내일 학습 할 것은 무엇인지 

 

데이터 리터러시 강의 듣기