[SQL๋ฌธ์ œํ’€๊ธฐ - Advent of SQL 2025 ๐ŸŽ…] ์—ฐ๋„๋ณ„ ๋ฐฐ์†ก ์—…์ฒด ์ด์šฉ ๋‚ด์—ญ ๋ถ„์„ํ•˜๊ธฐ

silver's avatar
Dec 19, 2025
[SQL๋ฌธ์ œํ’€๊ธฐ - Advent of SQL 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

silver