inblog logo
|
silver
    SQL문제풀기

    [SQL문제풀기] 온라인 쇼핑몰의 월 별 매출액 집계

    silver's avatar
    silver
    Mar 11, 2025
    [SQL문제풀기] 온라인 쇼핑몰의 월 별 매출액 집계
    Contents
    문제SQLite

    문제

    solvesql.com
    solvesql.com
    https://solvesql.com/problems/shoppingmall-monthly-summary/

    SQLite

    내가 작성한 정답

    select *, ordered_amount+canceled_amount total_amount from (select strftime('%Y-%m',order_date) order_month , sum(case when quantity>0 then quantity*price end) ordered_amount, sum(case when quantity<0 then quantity*price end) canceled_amount from orders o join order_items i on o.order_id = i.order_id group by order_month) order by order_month

    내가 작성한 정답2

    💡
    Sign는 주어진 숫자의 부호를 반환하는 함수로 양수일 경우 1 0일 경우 0 음수일 경우 -1를 반환한다.
    select *, ordered_amount+canceled_amount total_amount from (select strftime('%Y-%m',order_date) order_month , sum(case sign(quantity) when 1 then quantity*price end) ordered_amount, sum(case sign(quantity) when -1 then quantity*price end) canceled_amount from orders o join order_items i on o.order_id = i.order_id group by order_month) order by order_month
     
    Share article

    silver

    RSS·Powered by Inblog