Daily plan
๐์ค์
- SQL ์ฝ๋์นดํ
- 9์ 30๋ถ QCC
๐ฅ ์คํ
- QCC ํด์ค
- ๋น
๋ถ๊ธฐ D-1...........
๐ ์ ๋
- ์ค๋ ํ๋ฃจ๋ฅผ ๋น
๋ถ๊ธฐ์๊ฒ ๋ฐ์นฉ๋๋ค....
select id
from(
select id,
temperature,
lag(temperature) over(order by recordDate) yesterday_temp
from weather
) a
where temperature>yesterday_temp
์ต๋ํ ์๋ธ์ฟผ๋ฆฌ๋ฅผ ์ ์ฐ๋ ๋ฐฉ๋ฒ์ผ๋ก ํ์ด๋ด๊ณ ์ถ์๋๋ฐ,, ์ด ๋ฐฉ๋ฒ๋ฐ์ ๋ ์ค๋ฅด์ง ์์๋ค.
๊ทผ๋ฐ gptํํ
๋ฌผ์ด๋ดค๋๋ ์๋ ์๋ธ์ฟผ๋ฆฌ ์ฐ๋ ๊ฑฐ ๋ณด๋๊น ๊ฑ ์ด๋ ๊ฒ ํ๋ฉด ๋ ๊ฑฐ ๊ฐ๋คใ
WITH TempWithYesterday AS (
SELECT id, temperature,
LAG(temperature) OVER(ORDER BY recordDate) AS yesterday_temp
FROM weather
)
SELECT id
FROM TempWithYesterday
WHERE temperature > yesterday_temp;
์ด๊ฑด gpt๊ฐ ์ข์ํ๋ CTE ๋ฐฉ์^^
CTE(๊ณตํต ํ ์ด๋ธ ํํ์) ์ฌ์ฉ ์ ์ฅ์
- ๋ณต์กํ ์๋ธ์ฟผ๋ฆฌ๋ฅผ ๋ณ๋๋ก ๋ถ๋ฆฌํ์ฌ ๊ฐ๋ ์ฑ ํฅ์
- ํฐ ํ ์ด๋ธ์์ ์ฌ๋ฌ ๋ฒ ๊ฐ์ ์๋ธ์ฟผ๋ฆฌ๋ฅผ ์คํํ ๊ฒฝ์ฐ ์ฑ๋ฅ ์ต์ ํ ๊ฐ๋ฅ
- ๋ค๋ฅธ ์กฐ๊ฑด์ด ์ถ๊ฐ๋ ๋ ์ ์ง๋ณด์์ฑ์ด ๋์์ง
select unique_logins,
count(distinct employee_id) employee_count
from (select employee_id,
count(1) unique_logins
from logins
where login_result='SUCCESS'
and date_format(login_time,'%Y%m%d') between '20230701' and '20230930'
group by employee_id
) a
group by unique_logins
order by unique_logins
select e.employee_id, e.name, e.salary
from employee_salary e
join (select salary, rank() over(order by salary desc) as salary_rank
from employee_salary
group by salary) r
on e.salary = r.salary
where salary_rank=3
order by employee_id
ํํฐ๋์ dense rank๋ฅผ ์ด์ฉํ์
จ๋ค.
rank์ dense rank๋ ๋ชจ๋ ์ค๋ณต ์์๋ฅผ ๋์ผ ์์๋ก ํ์ํ๋ค๋ ์ ์ด ๊ฐ์ง๋ง,
rank๋ ์ค๋ณต ์์ ๋ค์์ ์ค๋ณต ๊ฐ์๋งํผ ๋จ์ด์ง ์์๋ก ๋ถ์ฌํ๊ณ
dense rank๋ ์ค๋ณต ์์ ๋ค์์ ์ค๋ณต ๊ฐ์์ ์๊ด์์ด ์์ฐจ์ ์ธ ์์๋ฅผ ๋ถ์ฌํ๋ค๋ ์ ์ด ๋ค๋ฅด๋ค.
์ฌ๊ธฐ์๋ salary๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ฐ์ salary์๋ ๊ฐ์ ์์๋ฅผ ๋ถ์ฌํ๊ณ , ์ค๋ณต์ ์๊ด์์ด ์์ฐจ์ ์ธ ์์๋ฅผ ๋ถ์ฌํ๋ฉด ๋๋ฏ๋ก dense rank๊ฐ ๋ ์ ์ ํ ๊ฒ ๊ฐ๋ค.
(์ด ๋ฌธ์ ์ ๊ฒฝ์ฐ์์๋ salary ๊ธฐ์ค์ผ๋ก groupbyํด์ ์์๋ฅผ ๋งค๊ธฐ๋ ๊ฒ๊ณผ ๋ง์ฐฌ๊ฐ์ง์ผ ๊ฒ ๊ฐ์)
๊ทธ๋ฆฌ๊ณ ํ์คํ with๋ฌธ์ ์ฐ๋ ๊ฒ ๊น๋ํ๋ฏ!
...์ด๋ผ๊ณ n๋ง๋ฒ์งธ ๋งํ๋ฉด์ ์ ๋ with๊ตฌ๋ฌธ ์์ฐ๋ ์ฌ๋...
with salary_ranked as(
select employee_id, name, salary,
dense_rank() over (order by salary desc) as rnk
from employee_salary
)
select employee_id, name, salary
from salary_ranked
where rnk=3
order by employee_id
select round(count(case when send_dep<>rec_dep then 1 end)/count(1),1) inter_department_msg_pct
from(
select *,
(select department
from employees e
where e.employee_id=m.sender_id) as send_dep,
(select department
from employees e
where e.employee_id=m.receiver_id) as rec_dep
from messages m) a
where send_dep is not null and rec_dep is not null
๋๋ฌด ์ง์ ๋ถํ๊ฒ ํผ ๊ฑฐ ๊ฐ์์ ๋ง์ ์ ๋ฆ..
๋ค์ ๋ด๋ from๋ฌธ์์ ์ ์ ๋ ๊ฒ ๋ณต์กํ๊ฒ ๋๋ฆฌ๋ฅผ ์ณ๋จ๋์ง ๋ชจ๋ฅด๊ฒ ๋ค.
๋ฌธ์ ํ๋ค๊ฐ ์ด์ง๋ฌ์์ง ์ ์ ์ํ๋ฅผ ์ฝ๋๋ก ํํํ ๋๋..
select
round(100.0 * SUM(case when e1.department != e2.department then 1 else 0 end) / count(*), 1) inter_department_msg_pct
FROM messages m
inner join employees e1 on m.sender_id = e1.employee_id
inner join employees e2 on m.receiver_id = e2.employee_id
์ด๋ ๊ฒ๋ง ํ๋ฉด ํจ์ฌ ๊ฐ๋จํ ๊ฒ์...
์๋ ์๊ฐ ์์ ๋ชป ํ์๋ค...ใ
- python์ window function(shift, rolling, expanding)์ ์ดํดํ๋ค.
- ์๊ณ์ด ๋ฐ์ดํฐ๋ฅผ ํ์ฉํ ํ์ ๋ณ์(์ด๋ ํ๊ท , ๋์ ํฉ) ์์ฑ์ ์ค์ตํ๋ค.
- ๋ฐ์ดํฐ ํ์
๋ณ ๋ค์ํ ์๊ด๊ด๊ณ๋ฅผ ์ดํด๋ณด๊ณ ์ค์ตํ๋ค.
df.shift(1) # ๋ค์ ๋ ์ง ๋ฐ์ดํฐ๋ฅผ ์์ผ๋ก ํ ์นธ ๋ก๊น
df.shift(-1) # ์์ ๋ ์ง ๋ฐ์ดํฐ๋ฅผ ๋ค๋ก ํ ์นธ ๋ก๊น
df.shift(periods=3, freq='D') # 3์ผ ์ด๋
df.shift(periods=3, freq='infer') # df์ ๋ ์ง๊ฐ๊ฒฝ๋ฅด ๋ถ์ํด์ ์ ๋นํ ์ฃผ๊ธฐ๋ฅผ ์ด๋
df.rolling(window=3).mean() # 3์ผ ์ด๋ ํ๊ท ๊ตฌํ๊ธฐ
df.rolling(window=3).sum() # 3์ผ ๋์ ํฉ ๊ตฌํ๊ธฐ
df.rolling(window=3, center=True, closed='left').mean()
# 3์ผ ์ด๋ํ๊ท ์ ์ค๊ฐ ํ ๊ธฐ์ค์ผ๋ก ๊ณ์ฐํ๊ณ , ์ผ์ชฝ ๊ฐ์ ํฌํจํ์ฌ ๊ณ์ฐ

๋๋์ด ์ค๋ ๋์ฉ๋ ์๊ธฐ๊ธฐ ์ฑ๊ณตํ๋ค,, ๋ฟ๋ฏํจ max
๊ณต๋ถ๋ ์ํ์๋ ์น๋ถ์์ด ์ ํ ์๋๋ฐ ์ด์ํ๊ฒ ์ด๋ฐ๊ฑฐ์ ๋ชฉ์จ๊ฑธ๊ฒ ๋๋ ์ด์ ๊ฐ ๋ญ๊น..
ํ๋์ SQL ์ ๋๊ณ ์์๋๋ ๊ฐ ๋ค ์์๋ค. ๋ค์ ์ฝ๋์นดํ ์ด์ฌํ ํ์! ใ .ใ
์ด๋ฒ์ฃผ์ ํ๋น์ ๋๋ฌด ์๋ด์ ๊ทธ๋ฐ์ง ์ ์ ์ํ๊ฐ ์ข ์ด์ํ๋ค. ์ ๋ ์์์ง๊ณ ๊ธ์๋ฅผ ์ฝ์ด๋ ์ ์ฝํ๊ณ ,, ํ๋ฃจ์ 10๋ถ์ด๋ผ๋ ๋๊ฐ์ ์ฐ์ฑ ์ ํ๋์ง ํ๋น์ ๋ณด๊ณ ์์ผ๊ฒ ๋ค.
๊ทธ๋ฆฌ๊ณ ์ด์ ์ง์ง ์ผ์ฐ ์๊ธฐ!!!
11์ ๋๊ธฐ๋ ์ ์ ์๋ ์์ ์๋ ์ ์ ์ด ์ ๋ง ๊ฑด๊ฐํ ๊ฒ ๋๊ปด์ก์๋๋ฐ ์์ฆ ์๋ฒฝ 2~3์์ ์๋ ๊ฒ ์ต๊ด์ด ๋๋๋ ๋ฉํ์ด ๋ถ์์ ํด์ง๊ณ ์๋ ๊ฑฐ ๊ฐ์,,
๋ด์ผ ๋น
๋ถ๊ธฐ ์ํ์ธ๋ฐ ๊ธฐ๋๊ฐ ์์ด์ ๋ถ์ํ์ง๋ ์๋คใ
ADsP๋๋ ๋ฑ ์ด๋ฐ ๊ธฐ๋ถ์ด์๋ ๊ฑฐ ๊ฐ์๋ฐ,, ๊ทธ๋์ฒ๋ผ ํ๋์ด ๋ด ํธ์ด ๋ค์ด์ฃผ๊ธธ ๋ฐ๋ผ์ง๋ง ๋๋ฌด ์์ฌ์๋ ๊ฑฐ ๊ฐ์์ ๊ฑ ํดํํจใ
๊ทธ๋๋ ๋๊น์ง ํ์ดํฐ์ด์!