TIL
231130
30303
2023. 11. 30. 20:28
728x90
엑셀보다 쉽고 빠른 SQL
강의 주제 | 배운 내용 | 예시 코드 | 비고 | |
3-6 | [실습] 조건문으로 서로 다른 식을 적용한 수수료 구해보기 | 실습1. 지역과 배달 시간을 기반으로 배달 수수료 구하기 실습2. 주문 시기와 음식 수를 기반으로 배달 할증료 구하기 |
[실습1] select case when delivery_time >30 then price*0.1 when delivery_time >25 then price*0.05 from food_orders -> select case when delivery_time >30 then price*0.1*if(addr like '%서울%', 1.1, 1) when delivery_time >25 then price*0.05*if(addr like '%서울%', 1.1, 1) else 0 end "수수료", restaurant_name , order_id , price, delivery_time , addr from food_orders [실습2] select case when day_of_the_week ='weekday' then 3000*if(quantity >3, 1.1, 1) when day_of_the_week ='weekend' then 3500*if(quantity >3, 1.1, 1) end "배달할증료", restaurant_name , order_id , day_of_the_week , quantity from food_orders fo |
- 선행에서 30분 초과인 케이스를 제했기 때문에 25~30 으로 두번째 케이스 표기할 필요 x - 배달 시간 별로 나눈 후(case when) , 지역 조건을 추가(if) |
3-7 | sql 문에 문제가 없는데 오류가 나는 경우(data type 오류 해결) | 문자, 숫자 데이터 타입 분류, 데이터 타입 변경 | 문자-> 숫자 Cast(if(rating=’Not given’, ‘1’, rating) as decimal) 숫자-> 문자 Concat(restaurant_name, ‘-‘, cast(order_id as char)) |
|
3-8 | 숙제 | SELECT case when day_of_the_week ='Weekday' then if(delivery_time>=25, '지각','정상') when day_of_the_week ='Weekend' then if(delivery_time>=30,'지각','정상') end "배달시간", order_id , delivery_time , day_of_the_week from food_orders |
||
4-1 | 포맷 변경과 조건문 복습 | *replace 다른 문자로 변경 *substring 특정 문자 추출 *concat 여러 문자 합하여 포맷팅 *조건문 if/ case when |
||
4-2 | 여러번의 연산을 한 번의 sql 문으로 수행하기 (subquery) | - 여러 번의 연산 수행 - 조건문에 연산 결과 사용 -조건문에 쿼리 결과 사용 |
SELECT price/quantity from (SELECT price, quantity from food_orders ) |
(1+1)*2 와 같은 느낌이라는데 |
4-3 | [실습] User segmentation와 조건별 수수료를 subquery로 결합해 보기 | 실습 1. 음식점 평균 단가별 segmatation 진행, 그룹에 따라 수수료 연산 실습 2. 음식점 지역과 평균 배달시간으로 세그멘테이션 |
[실습1] select restaurant_name, price_per_plate*ratio_of_add "수수료" from ( select restaurant_name, case when price_per_plate<5000 then 0.005 when price_per_plate between 5000 and 19999 then 0.01 when price_per_plate between 20000 and 29999 then 0.02 else 0.03 end ratio_of_add, price_per_plate from ( select restaurant_name, avg(price/quantity) price_per_plate from food_orders group by 1 ) a ) b [실습2] SELECT restaurant_name, sido, avg_delivery_time, case when avg_delivery_time<=20 then '<=' when avg_delivery_time>20 and avg_delivery_time<=30 then '20<x<=30' else '>30' end delivery_time_segment from (select restaurant_name , SUBSTR(addr,1,2) sido, avg(delivery_time)avg_delivery_time from food_orders group by 1,2) a |
가장 가운데 괄호부터 진행, 범주별 계산 시 group by 1,2 == 1,2 컬럼을 기준으로.. |
4-4 | [실습] 복잡한 연산을 subquery로 수행하기 | 실습 1. 음식 타입별 지역별 총 주문수량과 음식점 수를 연산하고, 주문 수량과 음식점수 별 수수료율을 산정하기 실습 2. 음식점의 총 주문수량과 주문 금액을 연산하고 주문 수량을 기반으로 수수료 할인율 구하기 |
[실습1] select cuisine_type, total_quantity, count_res, case when count_res>=5 and total_quantity>=30 then 0.005 when count_res>=5 and total_quantity<30 then 0.008 when count_res<5 and total_quantity>=30 then 0.01 when count_res<5 and total_quantity<30 then 0.02 end rate from (select cuisine_type , sum(quantity) total_quantity, count(distinct restaurant_name) count_res from food_orders [실습 2] select restaurant_name , sum_price, sum_quantity, case when sum_quantity<=5 then 0.1 when sum_quantity>15 and sum_price>=300000 then 0.005 else 0.01 end discount_rate from (select restaurant_name , sum(price) sum_price, sum(quantity) sum_quantity from food_orders group by 1)a |
TIL(Today I Learned) 작성법에 따른 작성.
1. 어떤 문제가 있었는지
문제1 지난 주에 배운 강의 복습 안 해서 까먹음,
문제2 sql 코드카타 문제 1번 동물 보호소에 들어온 동물 중, 이름이 있는 동물의 ID를 조회하는 SQL 문을 작성해주세요.
=>null 값 제외 하는 법 몰루.
2. 내가 시도해본 것들
시도1 워드 강의록 찾아봄
시도2 구글링
3. 어떻게 해결했는지
위와같음
4. 무엇을 새롭게 알았는지
1. desc, distinct 등 복습 할 것 많음
2. 결측값 제외 방법 => Select a 컬럼 From b 테이블 where not a 컬럼 is null