๋ฐฐ์ก ์์ ์ผ ์์ธก ์ฑ๊ณต๊ณผ ์คํจ
-- ์ต์ข
SELECT strftime('%Y-%m-%d', order_purchase_timestamp) as "purchase_date",
SUM(CASE WHEN order_estimated_delivery_date >= order_delivered_customer_date THEN 1 ELSE 0 END) as "success",
SUM(CASE WHEN order_estimated_delivery_date < order_delivered_customer_date THEN 1 ELSE 0 END) as "fail"
FROM olist_orders_dataset
WHERE strftime('%Y-%m-%d', order_purchase_timestamp) LIKE '2017-01%'
GROUP BY 1
ORDER BY 1 asc;
# ์ ๊ทผ ๋ฐฉ์
1. success์ fail ์ผ์ด์ค๋ฅผ ๊ฐ๋ณ ์ปฌ๋ผ์ผ๋ก ๋ง๋ค์ด์ผ๊ฒ ๋ค.
2. SUM (ํน์ COUNT๋ ์๊ด์์) ์ง๊ณ ํ ์ผ์๋ณ ๊ทธ๋ฃนํํด์ผ๊ฒ ๋ค.
3. ๊ธฐํ ์กฐ๊ฑด๋ค ์ถฉ์กฑ e.g., 17๋ 1์ ๋ฐ์ดํฐ๋ง, ๋ ์ง ์ค๋ฆ์ฐจ์
# ๋ฐฐ์ด์
1. SQLite - strftime
์ฒ์์ sqlite ๋ฌธ๋ฒ๋ง ๋๋ ์ค ๋ชจ๋ฅด๊ณ DATE_FORMAT์ ์ฌ์ฉํ๋ค๊ฐ Syntax Error ๋ ์ ๋นํฉํ๋ค.
์ฒ์ ์จ ๋ณด๋ ๋ฌธ๋ฒ์ด๋ฏ๋ก ์ถ์ธกํด์ ๋๋ ค๋ง์ถ๋ค๊ณ ๋ ์ผ์ด ์๋๋ผ์ ๋ฐ๋ก ๊ฐ์ด๋ ๋ฌธ์๋ฅผ ์ฐพ์๋ค.
๋นจ๋ฆฌ + ์ ์ ์์ด ํธ๋๋ผ ์ ๋๋ก ์ ์ฝ๊ณ strftime์ผ๋ก ์ผ๋๋ฐ, date๋ก ํ์ด๋ ์๊ด์์๋ค.
Date And Time Functions
1. Overview SQLite supports seven scalar date and time functions as follows: date(time-value, modifier, modifier, ...) time(time-value, modifier, modifier, ...) datetime(time-value, modifier, modifier, ...) julianday(time-value, modifier, modifier, ...) un
www.sqlite.org
2. SQLite - CASE
๋๋๊ฒ๋ IF๋ฌธ์ด ์๋ค. ๊ณต์ ๋ฌธ์์๋ CASE๋ฅผ ์ฌ์ฉํ๋ผ๊ณ ๋์ ์๋ค.
SQL Language Expressions
INTEGER When casting a BLOB value to INTEGER, the value is first converted to TEXT. When casting a TEXT value to INTEGER, the longest possible prefix of the value that can be interpreted as an integer number is extracted from the TEXT value and the remaind
www.sqlite.org
# ๋๋์
์๊ฐ์ ์กฐ๊ธ ๋ ํจ์จ์ ์ผ๋ก ํ๋ ค๊ณ ๋ ธ๋ ฅํด์ผ ํ๋ค. ๊ฐ๋ น ์ด๋ฒ ๋ฌธ์ ์์ ์ฒ์์๋ CASE WHEN ๊ตฌ๋ฌธ์ ์ด๋ฐ ์์ผ๋ก ์์ฑํ์๋ค.
CASE WHEN (order_estimated_delivery_date - order_delivered_customer_date) >= 0 THEN 1 ELSE 0 END
๊ตณ์ด ์ด๋ด ์ด์ ๊ฐ ์๋ค. ๊ทธ๋ฆฌ๊ณ ์ ์ด์ ๋ฌธ๋ฒ์ ์ผ๋ก ๋ถ๊ฐ๋ฅํ๋ค. sqlite์์๋ ๋ ์ง ๊ฐ์ ์ฐ์ ์ฐ์ฐ์(+, - ๋ฑ)๋ฅผ ์ง์ ์ฌ์ฉํ ์ ์๋ค. ๋ ๋ ์ง ๊ฐ ์ฐจ์ด๋ฅผ ์ผ ๋จ์๋ก ๊ณ์ฐํ๋ ค๋ฉด julianday ํจ์๋ฅผ ์ฌ์ฉํ๋ ๋ฑ์ ๋ฐฉ๋ฒ์ด ์๋ค.
mysql ๋ฌธ๋ฒ๋ง ์ฐ๋ค๊ฐ sqlite๋ฅผ ๋ง๋ฅ๋จ๋ ค์ ๋นํฉํ๊ธฐ๋ ํ์ง๋ง, ๋ฌธ๋ฒ์ด ๊ฐ์ ๊ฑฐ๋ผ๋ ํ์ ์ด ์์ด์ ๋ค์ ์๊ฐํด๋ธ ๋ฐฉ๋ฒ์ด ๊ฒฐ๊ตญ ์ ๋ต์ด์ด์ ๋คํ์ด์๋ค.
'๐งฑ SQL > ๐ป ์ฟผ๋ฆฌ ํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
240102 TUE ๋์ฌ ํ์๊ฐ ๋ง์ ์๋์ฐจ๋ค์ ์๋ณ ๋์ฌ ํ์ ๊ตฌํ๊ธฐ (0) | 2024.01.02 |
---|---|
231227 WED ์ฐ์ ์ ์๊ฑฐํธ๊ฐ ๋ด๊ธด ์ฅ๋ฐ๊ตฌ๋ (0) | 2023.12.27 |
231226 TUE ์ฝ๋์นดํ ๋ฆฌ๋ทฐ (1) | 2023.12.26 |
231224 SUN ์ง๊ณํจ์ (0) | 2023.12.24 |