[SQL๋ฌธ์ ํ๊ธฐ - Advent of SQL 2025 ๐ ] ์ฐ๋๋ณ ๋ฐฐ์ก ์ ์ฒด ์ด์ฉ ๋ด์ญ ๋ถ์ํ๊ธฐ
Dec 19, 2025
๋ฌธ์
๋ด๊ฐ ์์ฑํ ์ ๋ต
MySQL
select year(purchased_at) year,
sum(case when is_returned and shipping_method = 'Standard' then 2
when is_returned then 1
when shipping_method = 'Standard' then 1 end) standard,
sum(case when shipping_method = 'Express' then 1 end) express,
sum(case when shipping_method = 'Overnight' then 1 end) overnight
from transactions
where is_online_order
group by year(purchased_at)
order by 1;select year(purchased_at) year,
sum(case when is_returned then 1 end) +
sum(case when shipping_method = 'Standard' then 1 end) standard,
sum(case when shipping_method = 'Express' then 1 end) express,
sum(case when shipping_method = 'Overnight' then 1 end) overnight
from transactions
where is_online_order
group by year(purchased_at)
order by 1;PostgreSQL
: to_char๋ก ๋
๋ ์ถ๋ ฅํ๋ฉด ์ค๋ต์ฒ๋ฆฌ
select extract(year from purchased_at) as year,
sum(case when is_returned and shipping_method = 'Standard' then 2
when is_returned then 1
when shipping_method = 'Standard' then 1 end) standard,
sum(case when shipping_method = 'Express' then 1 end) express,
sum(case when shipping_method = 'Overnight' then 1 end) overnight
from transactions
where is_online_order
group by extract(year from purchased_at)
order by 1;select extract(year from purchased_at) as year,
sum(case when is_returned then 1 end) +
sum(case when shipping_method = 'Standard' then 1 end) standard,
sum(case when shipping_method = 'Express' then 1 end) express,
sum(case when shipping_method = 'Overnight' then 1 end) overnight
from transactions
where is_online_order
group by extract(year from purchased_at)
order by 1;SQLite
select strftime('%Y',purchased_at) year,
sum(case when is_returned and shipping_method = 'Standard' then 2
when is_returned then 1
when shipping_method = 'Standard' then 1 end) standard,
sum(case when shipping_method = 'Express' then 1 end) express,
sum(case when shipping_method = 'Overnight' then 1 end) overnight
from transactions
where is_online_order
group by strftime('%Y',purchased_at)
order by 1;select strftime('%Y',purchased_at) year,
sum(case when is_returned then 1 end) +
sum(case when shipping_method = 'Standard' then 1 end) standard,
sum(case when shipping_method = 'Express' then 1 end) express,
sum(case when shipping_method = 'Overnight' then 1 end) overnight
from transactions
where is_online_order
group by strftime('%Y',purchased_at)
order by 1;Share article