-- 月額換算せず3金額を足して12倍 select sum(fee)*12 from plans; -- 支払が発生する月だけを並べて合計 with recursive m(n) as (values(1) union all select n+1 from m where n<12) select sum(fee) from m join plans on n between firstmonth and lastmonth; -- 年払いが重なる4月 select sum(fee) from plans where 4 between firstmonth and lastmonth; -- 解約後の7月 select sum(fee) from plans where 7 between firstmonth and lastmonth;