본문 바로가기
SQL

12/27 - SQL 심화학습 2일차

by PETRA_94 2023. 12. 27.

[기억할 개념]

  1. 값이 잘못되어 있을 때:
    case when 가장 적은 값일 경우, then ~
             when 가장 큰 값일 경우, then ~ 
  2. 데이터  갯수 : COUNT(컬럼) * 컬럼명  대신 1 혹은 * 사용  가능 
    몇개의  값을  가지고  있는지  구할  때 : DISTINCT
  3. 날짜 함수: date-format(date(date), '%n'), "0"
    a. 년 : Y (4자리), y(2자리)
    b. 월 : M, m
    c. 일 : d, e
    d. 요일 : w

    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') "요일"  (*요일을 숫자로 표현, 0이 일요일)
    from payments
  4. window 함수
    ⓛ 기본 구조
    window_function(argument) over (partition by 그룹 기준 컬럼 order by 정렬 기준)
    window_function : 기능명을  사용(sum, avg 와  같이  기능명) 
    argument : 함수에  따라  작성하거나  생략
    partition by : 그룹을  나누기  위한  기준으로, group by 절과  유사
    order by : window function 을  적용할  때  정렬  할  컬럼  기준

    ② rank(순위): rank () over(partition by 기준 컬럼 by) 별명 
    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(누적합): sum() over(partition by 기준 컬럼 by) 별명 
    select cuisine_type, 
    restaurant_name, 
    order_count,
    order_count/sum_cuisine_type*100 "음식점의 차지비율", 
    cumulative_sum "누적합"
    from 
    (
    select cuisine_type, 
    restaurant_name, 
    order_count,
    sum(order_count) over (partition by cuisine_type) sum_cuisine_type,
    sum(order_count) over (partition by cuisine_type order by order_count, restaurant_name) cumulative_sum
    from 
    (
    select cuisine_type, restaurant_name, count(1) order_count 
    from food_orders
    group by 1, 2 
    )a
    )b
    order by 1, 5
  5. Pivot Table:
    ① 기본 구조
    select 첫 행 기준 컬럼
    mas(if(가정, 값이 맞을 때, 틀렸을 때)) "카테고리"
    from
    (
    두번째 수식
    ) a
    group by 1
    => 연산 수식(max, sum, avg 등) 마지막 하단에 group by로 꼭 묶어줄 것!

    ② 예시
    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

 

[코드카타]

  • 내가 쓴 raw code:
select animal_id, name
from animal_ins
where intake_condition='Aged'
order by animal_id
  • 튜터님이 쓴 raw code:
select animal_id, name
from animal_ins
where intake_condition !='Aged'
order by animal_id

 

튜터님 답변) aged는 나이가 들었음을 표현 (영어를 잘 모르는 게 여기서 탄로 남..ㅋ)
● 조건이 해당 되지 않을 경우 문법: '!=' 부호 사용
● 코드카타 본문 내용 정답과 실제 정답이 다른 경우 다수이므로, 코드 작성 후 채점하는 것이 가장 중요!

 

[배운 점]

  • SQL 초반에는 쉬웠으나, 점점 구문이 길어질수록 분석해내는 게 쉽지 않다.
  • 구문을 작성하는 것도 어렵지만, 해석하는 것도 다소 버퍼링이 걸려서 어렵게 느껴진다.
  • 이제 SQL 기본 강의를 끝내고, SQLD 자격증 대비반 강의를 듣기 시작했다.
    16주차까지 있어, 다소 빡세게 느껴지지만 그래도 듣다 보면 잘 될 것 같다.