https://solvesql.com/problems/flow-and-stock/
-- 연도별로 새롭게 소장하게 된 작품 수(유량), 연도별 누적 소장 작품 수(저량)
-- 저량지표는 변화가 없는 연도는 출력되지 않아야 됨.(변화가 없는 연도는 출력하지 말라는게 잘 이해가 안됨. 그리고 이거 무시해서 해도 정답 처리됨.)
-- 소장 년도 정보가 없으면 제외
-- Acquisition year 컬럼을 기준으로 오름차순
WITH acq AS(
SELECT
SUBSTRING(acquisition_date,1,4) AS "Acquisition year",
COUNT(artwork_id) AS "New acquisitions this year (Flow)"
FROM
artworks
GROUP BY
"Acquisition year"
HAVING 1=1
AND "Acquisition year" IS NOT NULL
ORDER BY
"Acquisition year"
)
SELECT
*,
SUM("New acquisitions this year (Flow)") OVER (ORDER BY "Acquisition year") AS "Total collection size (Stock)"
FROM
acq