select td.报考课目 as 科目,td.报名时间 as 时间,sum(isnull(报名人数,0)) as 报名人数,sum(isnull(交费人数,0)) as 交费人数
from(
select 报考课目,报名时间,count(*) as 报名人数,0 as 交费人数
from bmb
group by 报考课目,报名时间
union all
select 报考课目,交费时间,0 as 报名人数,count(*) as 交费人数
from bmb
group by 报考课目,交费时间
) tb right join (select *
from(select distinct 报名时间
from bmb
union
select distinct 交费时间
from bmb) ta,
(select distinct 报考课目
from bmb) tc) td on tb.报考课目 = td.报考课目 and tb.报名时间 = td.报名时间
group by td.报考课目,td.报名时间
order by td.报考课目,td.报名时间