스파르타코딩클럽

Day 6 Today I Learned (2)

글 잘 쓰는 독종 2024. 9. 24. 17:41

오늘 배운 내용

엑셀보자 쉽고 빠른 SQL

3-1 3주차 오늘 배울 것

3-2 업무 필요한 문자 포맷이 다를 때

replace(바꿀 컬럼, 현재 값, 바꿀 값)

select restaurant_name "원래 상점명",
	replace(restaurant_name, 'Blue', 'Pink') "바뀐 상점"
from food_orders
where restaurant_name like '%Blue Ribbon%'

substr(조회 할 컬럼, 시작 위치, 글자 수)

SELECT addr "원래 주소",
substr(addr, 1, 2) "시도"
from food_orders
where addr like '%서울특별시%'

concat

select restaurant_name "원래 이름",
	addr "원래 주소",
	concat('[', substr(addr, 1, 2), ']', restaurant_name) "바뀐 이름"
from food_orders
where addr like '%서울%'

3-3 문자 데이터를 바꾸고, group by 사용하기

select substr(addr, 1, 2) "지역",
	cuisine_type,
	avg(price) "평균 금액"
from food_orders
where addr like "%서울%"
group by 1, 2

3-4 조건에 따라 포맷을 다르게 변경해야 한다면

if(조건, 조건을 충족할 때, 조건을 충족하지 못할 때)

select restaurant_name,
	cuisine_type "원래 음식 타입",
	if(cuisine_type='Korean', '한식', '기타') "음식 타입"
from food_orders

case when

select case when cuisine_type ='Korean' then '한식'
		when cuisine_type in ('Japanese', 'Chinese') then '아시아'
		else '기타' end "음식타입",
		cuisine_type 
from food_orders

3-5 SQL로 간단한 User segmentation 해보기

 

느낀점

SQL 강의를 본격적으로 들어보았다. 문법은 파이썬에 가깝다는 인상을 받았다. group by 문이 아직 약간 헷갈린다