๋ฌธ์
https://solvesql.com/problems/day-of-furniture/
https://solvesql.com/problems/day-of-furniture/
solvesql.com
์ฝ๋ ์ ๋ฌธ
1์ฐจ ์๋
with
calc as (
SELECT
order_date,
count(*) AS total_orders,
count(
CASE
WHEN category = 'Furniture' THEN 1
ELSE 0
END
) AS furniture_orders
FROM
records
GROUP BY
order_date
)
select
order_date,
furniture_orders as furniture,
round(((furniture_orders) / (total_orders) * 100), 2) as furniture_pct
from
calc
where total_orders >= 10
and
(furniture_orders * 100) / total_orders >= 40
order by 3 desc, 1
์ ๋ต ์ฝ๋
with
calc as (
SELECT
order_date,
COUNT(distinct order_id) AS total_orders,
COUNT(distinct
CASE
WHEN category = 'Furniture' THEN order_id
ELSE null
END
) AS furniture_orders
FROM
records
GROUP BY
order_date
)
select
order_date,
furniture_orders as furniture,
round(((furniture_orders + 0.00) / (total_orders + 0.00) * 100), 2) as furniture_pct
from
calc
where total_orders >= 10
and
(furniture_orders * 100) / total_orders >= 40
order by 3 desc, 1
ํต์ฌ ์์ง
1. ๊ณ ์ ํ order_id๋ก ์ฃผ๋ฌธ ์๋ฅผ ์ธ์ผ ํ๋ค.
๊ฐ์ order_id๋ก ์ฌ๋ฌ ๊ฐ์ ์ฃผ๋ฌธ record๊ฐ ์กด์ฌํ๊ธฐ ๋๋ฌธ์ distinct order_id๋ก ์ฃผ๋ฌธ ์๋ฅผ ์ธ์ผ ํ๋ค.
2. SQLite๋ ๋๋์ ์ ํ ์, ์ ์๋ก ๊ฒฐ๊ณผ๊ฐ ๋์จ๋ค
์ ์๋ฅผ ๋๋๊ธฐํ๋ฉด ์ ์๋ก ๋์ค๊ธฐ ๋๋ฌธ์ floatํ์ผ๋ก ๋ง๋ค์ด์ฃผ๊ธฐ ์ํด ( + 0.00 )์ ํด์ค์ผ ํ๋ค.
๋๋ ์
๋ค๋ฅธ ๋ถ๋ค์ ์ฝ๋ ๋ต์์ ๋ดค์ ๋, ์๋ธ์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฉํ๊ฑฐ๋ ๋ ๊น๋ํ๊ฒ ํผ ๊ฒ๋ค๋ ๋ดค์ผ๋
๋๋ with ์ ๋ก ๊ตฌ๋ถํด์ ์ฟผ๋ฆฌ ์ง๋ ๊ฑธ ์ ํธํ๊ธฐ ๋๋ฌธ์ ์์ ๋ต์๋๋ก ์์ฑ์ ํ๋ค.
๊ทผ๋ฐ with ์ ์ ์ฌ์ฉํ๋ฉด ํ์คํ ์ฟผ๋ฆฌ ์ง๋ ๋ฐ ์๊ฐ์ด ๋ง์ด ์๋ชจ๋๋ ๊ฒ ๊ฐ๋ค.
์ข ๋ ๊น๋ํ๊ฒ ํจ์จ์ ์ผ๋ก ์ง๋ ๋ฐฉ๋ฒ์ ์๊ฐํด๋ด์ผ๊ฒ ๋ค.
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))+0.00) / (count(distinct(order_id))+0.00)*100,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;
^ ๊ฐ์ฅ ๊น๋ํ ์ฝ๋ ๋ต์์ธ ๋ฏ
https://milkyspace.tistory.com/90
[solvesql] ๊ฐ๊ตฌ ํ๋งค์ ๋น์ค์ด ๋์๋ ๋ ์ฐพ๊ธฐ
https://solvesql.com/problems/day-of-furniture/ solvesql © Copyright 2021-2022 solvesql.com solvesql.com ๋ฌธ์ ๋ ์์ ๊ฐ๋ค. ๊ฐ๊ตฌ๊ฐ ํ๋ฆฐ ์์ ๋น์จ์ ๊ตฌํด์ฃผ๋ฉด๋๋๋ฐ sqlite๋ฅผ ๋ง์ด ์์จ๋ด์ ๋ชฐ๋๋ค. ๋๋๊ธฐ๋ฅผ ํ๋ฉด ์ ์
milkyspace.tistory.com
^ ์ฌ๊ธฐ์ ํผ์๋ค !