select
    d.部门,
    d.人,
    sum(case 
          when a.销售价格>c.成本价格
            then b.奖励系数A*a.销售数量+(a.销售价格-c.成本价格)*b.额外奖励系数
          when a.销售价格<c.成本价格
            then b.奖励系数B*a.销售数量
          when a.销售价格=c.成本价格
            then b.奖励系数A*a.销售数量
        end)
from
    a,b,c,d
where
    a.销售单号=d.销售单号
    and
    a.X=c.X
    and
    b.部门 = d.部门
group by
    d.部门,d.人