반응형
자동차 대여 기록 별 대여 금액 구하기
https://school.programmers.co.kr/learn/courses/30/lessons/151141
WITH value AS (SELECT HISTORY_ID, DAILY_FEE, CAR.CAR_TYPE, DATEDIFF(END_DATE, START_DATE) + 1 AS DATE_DIFF,
CASE
WHEN DATEDIFF(END_DATE, START_DATE) + 1 >= 90 THEN '90일 이상'
WHEN DATEDIFF(END_DATE, START_DATE) + 1 >= 30 THEN '30일 이상'
WHEN DATEDIFF(END_DATE, START_DATE) + 1 >= 7 THEN '7일 이상'
ELSE 'NONE'
END
AS DURATION_TYPE
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY HIST
LEFT JOIN CAR_RENTAL_COMPANY_CAR CAR
ON CAR.CAR_ID=HIST.CAR_ID
WHERE CAR.CAR_TYPE='트럭')
SELECT value.HISTORY_ID, (value.DAILY_FEE * value.DATE_DIFF * (100-(SUBSTRING_INDEX(IFNULL(DISC.DISCOUNT_RATE, 0), '%', 1))) / 100) AS FEE
FROM value
LEFT JOIN CAR_RENTAL_COMPANY_DISCOUNT_PLAN DISC
ON value.DURATION_TYPE=DISC.DURATION_TYPE AND value.CAR_TYPE=DISC.CAR_TYPE
WHERE value.CAR_TYPE='트럭'
ORDER BY FEE DESC, HISTORY_ID DESC
처음엔 HISTORY 테이블에서 렌탈 기간을 숫자로 바꾸고, DISCOUNT 테이블에서도 기간을 숫자로 바꿨는데 비교가 일치가 아니라 부등호라서 애매해진다.
렌탈 기간을 n일 이상으로 바꿔서 일치하는 구간의 할인율을 구해야한다.
자동차 대여 기록에서 장기/단기 대여 구분하기
https://school.programmers.co.kr/learn/courses/30/lessons/151138
SELECT HISTORY_ID, CAR_ID, DATE_FORMAT(START_DATE, '%Y-%m-%d') AS START_DATE, DATE_FORMAT(END_DATE, '%Y-%m-%d') AS END_DATE,
CASE WHEN DATEDIFF(END_DATE, START_DATE)+1 >= 30 THEN '장기 대여'
ELSE '단기 대여'
END
AS RENT_TYPE
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
WHERE MONTH(START_DATE)=9
ORDER BY HISTORY_ID DESC
취소되지 않은 진료 예약 조회하기
https://school.programmers.co.kr/learn/courses/30/lessons/132204
SELECT APNT.APNT_NO,
PT.PT_NAME,
APNT.PT_NO,
APNT.MCDP_CD,
DOC.DR_NAME,
APNT.APNT_YMD
FROM APPOINTMENT APNT
JOIN DOCTOR DOC ON APNT.MDDR_ID=DOC.DR_ID
JOIN PATIENT PT ON APNT.PT_NO=PT.PT_NO
WHERE APNT.APNT_CNCL_YN='N' AND APNT.MCDP_CD='CS' AND APNT.APNT_YMD LIKE '2022-04-13%'
ORDER BY APNT_YMD
조건별로 분류하여 주문상태 출력하기
https://school.programmers.co.kr/learn/courses/30/lessons/131113
SELECT ORDER_ID, PRODUCT_ID, DATE_FORMAT(OUT_DATE, '%Y-%m-%d') AS OUT_DATE,
CASE WHEN OUT_DATE IS NULL THEN '출고미정'
WHEN OUT_DATE < '2022-05-02' THEN '출고완료'
ELSE '출고대기'
END
AS 출고여부
FROM FOOD_ORDER
ORDER BY ORDER_ID
루시와 엘라 찾기
https://school.programmers.co.kr/learn/courses/30/lessons/59046
SELECT ANIMAL_ID, NAME, SEX_UPON_INTAKE
FROM ANIMAL_INS
WHERE NAME IN ('Lucy', 'Ella', 'Pickle', 'Rogan', 'Sabrina', 'Mitty')
이름에 el이 들어가는 동물 찾기
https://school.programmers.co.kr/learn/courses/30/lessons/59047
중성화 여부 파악하기
https://school.programmers.co.kr/learn/courses/30/lessons/59409
SELECT ANIMAL_ID, NAME,
CASE WHEN (SEX_UPON_INTAKE LIKE 'Neutered%' OR SEX_UPON_INTAKE LIKE 'Spayed%') THEN 'O'
ELSE 'X'
END
AS '중성화'
FROM ANIMAL_INS
ORDER BY ANIMAL_ID
오랜 기간 보호한 동물(2)
https://school.programmers.co.kr/learn/courses/30/lessons/59411
SELECT OUTS.ANIMAL_ID, OUTS.NAME
FROM ANIMAL_OUTS OUTS
INNER JOIN ANIMAL_INS INS
ON INS.ANIMAL_ID = OUTS.ANIMAL_ID
ORDER BY (OUTS.DATETIME - INS.DATETIME) DESC
LIMIT 2
카테고리 별 상품 개수 구하기
https://school.programmers.co.kr/learn/courses/30/lessons/131529\
SELECT LEFT(PRODUCT_CODE,2) AS CATEGORY, COUNT(LEFT(PRODUCT_CODE, 2)) AS PRODUCTS
FROM PRODUCT
GROUP BY CATEGORY
728x90
반응형
'Data > SQL' 카테고리의 다른 글
MySQL Transpose (0) | 2023.01.25 |
---|---|
[프로그래머스] SELECT (MySQL) (0) | 2023.01.24 |
[프로그래머스] GROUP BY (MySQL) (0) | 2023.01.22 |
[프로그래머스] JOIN (MySQL) (0) | 2023.01.22 |
[프로그래머스] IS NULL (MySQL, Oracle) (0) | 2023.01.21 |