select
    m05.ktktcd, m05.ktktnm,
    nvl(v3105.psum,0) psum3105,
    nvl(v3109.psum,0) psum3109,
    decode(m02d.htcls,0,'する','しない') htcls,
    sum(nvl(v3105.psum,0)) over () tpsum3105,
    sum(nvl(v3109.psum,0)) over () tpsum3109
from
    (select ktktcd,sum(psum) psum from afv3109 where ijan=:ijan group by ijan,ktktcd) v3109,
    (select ktktcd,sum(psum) psum from afv3105 where ijan=:ijan group by ijan,ktktcd) v3105,
    afm02d m02d, afm05 m05
where m05.ktktcd=m02d.ktktcd(+) and m02d.ijan(+)=:ijan
  and m05.ktktcd=v3105.ktktcd(+)
  and m05.ktktcd=v3109.ktktcd(+)
  and (v3105.psum is not null or v3109.psum is not null)
order by decode(ktktcd,:spktcd,'0'||ktktcd,'1'||ktktcd)