문제
https://solvesql.com/problems/flow-and-stock/
https://solvesql.com/problems/flow-and-stock/
solvesql.com
코드
select year as "Acquisition year",
acquisition as "New acquisitions this year (Flow)",
sum(acquisition) over (order by year) as "Total collection size (Stock)"
from
(select substring(acquisition_date, 1, 4) as year, count(distinct artwork_id) as acquisition
from artworks
group by 1
having substring(acquisition_date, 1, 4) is not null) as t
-- 7분 성공
문제 풀이
- 날짜에서 연도 추출하기
1. substring (컬럼명, 시작위치, 길이)
2. strftime ('%단위', 컬럼명) : '%Y'로 연도 추출
SQLite는 시간 함수로 strftime을 지원한다.
MySQL에서는 date_format을 사용하면 되겠다.
date_format (컬럼명, %단위')
느낀 점
SQLite와 MySQL 문법을 구별해서 외워놔야겠다 ..!

'Tools > - SQL' 카테고리의 다른 글
| [SQL] solvesql | 세 명이 서로 친구인 관계 찾기 | self join 활용하기, on절 조건 여러개 (0) | 2025.02.14 |
|---|---|
| [SQL] solvesql | 레스토랑 요일 별 구매금액 Top 3 영수증 (0) | 2025.02.14 |
| [SQL] solvesql | 게임 개발사의 주력 플랫폼 찾기 (0) | 2025.02.09 |
| [SQL] solvesql | 전력 소비량 이동 평균 구하기 | rows 와 range의 차이 (0) | 2025.02.09 |
| [SQL] solvesql | 스테디셀러 작가 찾기 (0) | 2025.02.09 |