2020년만 조회, 1/2/4/5/7 월에는 값이 들어 있다는 가정하에서 생각해보았습니다.
더 효율적인 방법은 다른분이 올려주실거라 믿으며..
create table test_val (target_year integer, target_month integer, target_val integer); insert into test_val values (2020, 1, 1000); insert into test_val values (2020, 2, 2000); insert into test_val values (2020, 4, 4000); insert into test_val values (2020, 5, 5000); insert into test_val values (2020, 7, 7000); select * from test_val; with a as ( select 2020 as target_year, 1 as target_month, 0 as target_val union select 2020 as target_year, 2 as target_month, 0 as target_val union select 2020 as target_year, 3 as target_month, 0 as target_val union select 2020 as target_year, 4 as target_month, 0 as target_val union select 2020 as target_year, 5 as target_month, 0 as target_val union select 2020 as target_year, 6 as target_month, 0 as target_val union select 2020 as target_year, 7 as target_month, 0 as target_val union select 2020 as target_year, 8 as target_month, 0 as target_val union select 2020 as target_year, 9 as target_month, 0 as target_val union select 2020 as target_year, 10 as target_month, 0 as target_val union select 2020 as target_year, 11 as target_month, 0 as target_val union select 2020 as target_year, 12 as target_month, 0 as target_val ), b as ( select test_val.* from test_val, a where test_val.target_month = a.target_month ), c as ( select * from a where NOT EXISTS (select * from b where a.target_month = b.target_month) ) select target_year, target_month, target_val from ( select * from b union select * from c ) as t order by target_month