Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[solvesql] 난이도 3, 4 문제 #52

Open
sieunnnn opened this issue Jan 24, 2024 · 2 comments
Open

[solvesql] 난이도 3, 4 문제 #52

sieunnnn opened this issue Jan 24, 2024 · 2 comments
Assignees
Labels
Level 3 레벨 3 문제풀이 Level 4 레벨 4 문제풀이 solvesql solvesql 문제풀이 입니다. SQL SQL 문제풀이

Comments

@sieunnnn
Copy link
Owner

solvesql 난이도 3 문제 풀이 입니다.

@sieunnnn sieunnnn added SQL SQL 문제풀이 Level 3 레벨 3 문제풀이 solvesql solvesql 문제풀이 입니다. labels Jan 24, 2024
@sieunnnn sieunnnn self-assigned this Jan 24, 2024
@sieunnnn
Copy link
Owner Author

sieunnnn commented Jan 25, 2024

지역별 주문의 특징

스크린샷 2024-01-25 오후 6 16 02

위의 결과를 아래와 같은 형식으로 만들어야 합니다.

스크린샷 2024-01-25 오후 6 16 13

이 경우, 아래와 같이 코드를 작성하면 됩니다.

SELECT
    SUB.region AS Region,
    SUM(
            CASE
                WHEN SUB.category = 'Furniture' THEN SUB.orderId
                END
        ) AS Furniture,
    SUM(
            CASE
                WHEN SUB.category = 'Office Supplies' THEN SUB.orderId
                END
        ) AS Office_Supplies,
    SUM(
            CASE
                WHEN SUB.category = 'Technology' THEN SUB.orderId
                END
        ) AS Technology
FROM
    (
        SELECT
            region,
            category,
            COUNT(DISTINCT order_id) AS orderId
        FROM
            records
        GROUP BY
            region,
            category
        ORDER BY region
    ) AS SUB
GROUP BY
    SUB.region;
스크린샷 2024-01-25 오후 6 16 34

@sieunnnn
Copy link
Owner Author

sieunnnn commented Feb 5, 2024

가구 판매의 비중이 높았던 날 찾기

소수점 처리를 위해 ROUND() 를 사용했는데, 일반적인 수식으로 계산하면 계속 정수형이 나옵니다.
계산 값이 소수점으로 나오기 위해서는 100 을 곱하는 것이 아닌 100.0 을 곱해야 합니다.

SELECT
    order_date,
    COUNT(
            DISTINCT (
        CASE
            WHEN category = 'Furniture' THEN order_id
            END
        )
        ) AS 'furniture',
    ROUND(
                    COUNT(
                            DISTINCT (
                        CASE
                            WHEN category = 'Furniture' THEN order_id
                            END
                        )
                        ) * 100.0 / COUNT(DISTINCT order_id),
                    2
        ) AS 'furniture_pct'
FROM
    records
GROUP BY
    order_date
HAVING
        COUNT(DISTINCT order_id) >= 10
   AND furniture_pct >= 40
ORDER BY
    furniture_pct DESC,
    order_date

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Level 3 레벨 3 문제풀이 Level 4 레벨 4 문제풀이 solvesql solvesql 문제풀이 입니다. SQL SQL 문제풀이
Projects
None yet
Development

No branches or pull requests

1 participant