Database
SQL λμ ν©κ³ ν¨μ: SUM OVER
___l_i_ *
2021. 3. 23. 15:28
λ¨Όμ μμκ°κΈ°!
- μ§μ μ΄λ‘μμΌλ‘ μμ± λ λΆλΆμ μλ΅ κ°λ₯ν λΆλΆμ λλ€.
- νΉμ μμ± λ³λ‘ ꡬλΆνκ³ μ ν λ(=GROUP BY μ κ°μ λμμ μν€κ³ μΆμ λ) PARTITION BYλ₯Ό μ¬μ©ν©λλ€.
SUM OVER
λμ ν©κ³λ₯Ό ꡬν©λλ€.
sum(λμ) over (partition by λμ, order by λμ)
μμ 1 ) PANAME ν μ΄λΈμ μ‘°ννμ¬ μ νμ½λ, ν맀μ , ν맀λ μ§, ν맀λ, ν맀κΈμ‘κ³Ό μ ν μ½λ λ³ κ° μ€ν μ΄μ ν©κ³ μΆλ ₯
select P_CODE,
P_STORE,
P_DATE,
P_QTY,
P_TOTAL,
sum(P_TOTAL) over (partition by P_CODE, P_STORE order by P_DATE, P_TOTAL) "TOTAL"
from PANMAE;
μμ 2 ) ν맀 λΉμ¨ ꡬνκΈ°
select P_CODE, P_STORE, P_QTY, sum(P_QTY) over ( ) "TOTAL", round((P_QTY / sum(P_QTY) over ( )) * 100, 2) "QTY_%"
from PANMAE
where P_CODE = 100;
-- * * OR * *
select P_CODE,
P_STORE,
P_QTY,
sum(P_QTY) over ( ) "TOTAL",
round(ratio_to_report(sum(P_QTY)) over () * 100, 2) "QTY_%2"
from PANMAE
where P_CODE = 100
group by P_CODE, P_STORE, P_QTY;