240722_TIL

J Lee·2024년 7월 22일
1

아무리 사소하더라도 배움이 없는 날은 없다.

SQL 코드카타

문제 링크
날짜 포맷을 변경하는 문제.
지금까지는 %Y-%m-%d 정도만 주로 활용했는데,
당연히 주(week)나 월(month)에 대한 포매팅 방법도 존재한다.

참고 링크

SELECT Date_format(day, "%W, %M %e, %Y") AS "day"
FROM   days;

문제 링크
MySQL의 period_diff 함수.
삽입된 두 인자의 개월 수 차이를 구하는 함수다.
PERIOD_DIFF(P1, P2)라고 쓰면 P1-P2를 계산해 준다.
따라서 이 문제에서는 (t1.date, t2.date)라고 쓴 것.

WITH temp
     AS (SELECT t.account_id,
                Date_format(day, '%Y%m') AS date,
                Sum(amount)              AS 'income',
                accounts.max_income
         FROM   transactions t
                LEFT JOIN accounts
                       ON accounts.account_id = t.account_id
         WHERE  t.type = 'Creditor'
         GROUP  BY t.account_id,
                   Date_format(day, '%Y%m')
         HAVING Sum(amount) > accounts.max_income)
SELECT t1.account_id
FROM   temp t1,
       temp t2
WHERE  t1.account_id = t2.account_id
       AND Period_diff(t1.date, t2.date) = 1
GROUP  BY 1
ORDER  BY 1;

문제 링크

WITH result
     AS (SELECT order_id,
                product_id,
                quantity,
                Max(quantity)                     AS "max",
                Sum(quantity) / Count(product_id) AS "avg"
         FROM   ordersdetails
         GROUP  BY order_id)
SELECT order_id
FROM   result
WHERE  max > (SELECT Max(avg)
              FROM   result);

오늘은 최종프로젝트로 정신없이 달려서
코드카타는 3문제만. 눈이 가물거리네🥲

profile
기본기를 소홀히 하지 말자

0개의 댓글

관련 채용 정보