select Z.业务员,Z.年度,
       (select sum(指标值) from 指标表 where z.业务员=业务员 and Z.年度=年度 and 指标='合同额') as 合同额指标,
       (select sum(完成指标) from 合同表 where z.业务员=业务员 and Z.年度=年度 ) as 合同额完成指标,
       (select sum(完成指标) from 合同表 where z.业务员=业务员 and Z.年度=年度 )*0.1/(select sum(指标值) from 指标表 where z.业务员=业务员 and Z.年度=年度 and 指标='合同额') as 合同额完成率,
       (select sum(指标值) from 指标表 where z.业务员=业务员 and Z.年度=年度 and 指标='回款') as 回款指标,      
       (select sum(完成指标) from 回款表 where z.业务员=业务员 and Z.年度=年度 ) as 回款完成指标,
       (select sum(完成指标) from 回款表 where z.业务员=业务员 and Z.年度=年度 )*0.1/(select sum(指标值) from 指标表 where z.业务员=业务员 and Z.年度=年度 and 指标='回款') as 回款完成率
from 指标表 Z
group by Z.业务员,Z.年度

解决方案 »

  1.   

    Try:
    select * from 
    (select '合计' as 业务员,'' as 年度,sum(合同额指标) as 合同额指标,sum(合同额完成指标) as 合同额完成指标,sum(合同额完成率) as 合同额完成率,
           sum(回款指标) as 回款指标,sum(回款完成指标) as 回款完成指标,sum(回款完成率) as 回款完成率
    from 
    (select Z.业务员,Z.年度,
           (select sum(指标值) from 指标表 where z.业务员=业务员 and Z.年度=年度 and 指标='合同额') as 合同额指标,
           (select sum(完成指标) from 合同表 where z.业务员=业务员 and Z.年度=年度 ) as 合同额完成指标,
           (select sum(完成指标) from 合同表 where z.业务员=业务员 and Z.年度=年度 )*0.1/(select sum(指标值) from 指标表 where z.业务员=业务员 and Z.年度=年度 and 指标='合同额') as 合同额完成率,
           (select sum(指标值) from 指标表 where z.业务员=业务员 and Z.年度=年度 and 指标='回款') as 回款指标,      
           (select sum(完成指标) from 回款表 where z.业务员=业务员 and Z.年度=年度 ) as 回款完成指标,
           (select sum(完成指标) from 回款表 where z.业务员=业务员 and Z.年度=年度 )*0.1/(select sum(指标值) from 指标表 where z.业务员=业务员 and Z.年度=年度 and 指标='回款') as 回款完成率
    from 指标表 Z
    group by Z.业务员,Z.年度) T
    union
    select Z.业务员,Z.年度,
           (select sum(指标值) from 指标表 where z.业务员=业务员 and Z.年度=年度 and 指标='合同额') as 合同额指标,
           (select sum(完成指标) from 合同表 where z.业务员=业务员 and Z.年度=年度 ) as 合同额完成指标,
           (select sum(完成指标) from 合同表 where z.业务员=业务员 and Z.年度=年度 )*0.1/(select sum(指标值) from 指标表 where z.业务员=业务员 and Z.年度=年度 and 指标='合同额') as 合同额完成率,
           (select sum(指标值) from 指标表 where z.业务员=业务员 and Z.年度=年度 and 指标='回款') as 回款指标,      
           (select sum(完成指标) from 回款表 where z.业务员=业务员 and Z.年度=年度 ) as 回款完成指标,
           (select sum(完成指标) from 回款表 where z.业务员=业务员 and Z.年度=年度 )*0.1/(select sum(指标值) from 指标表 where z.业务员=业务员 and Z.年度=年度 and 指标='回款') as 回款完成率
    from 指标表 Z
    group by Z.业务员,Z.年度) Z
    order by case when 业务员='合计' then 1 else 0 end