日期:2014-05-17  浏览次数:20690 次

求一个sql 等大神
select a.Number400,b._year ,b._Month ,a.FeeType, a.FixedFee,a.FeeTypeName,a.Fee1,a.Fee2,a.Fee3,a.Fee4,
b.Consumer1,b.Consumer2,b.Consumer3,b.Consumer4,
case 
when
(case a.FeeType
when 1 then a.Fee1*b.Consumer1+a.Fee2*b.Consumer2
when 2 then a.Fee3*b.Consumer3+a.Fee4*b.Consumer4
end) > a.FixedFee then (case a.FeeType
when 1 then a.Fee1*b.Consumer1+a.Fee2*b.Consumer2
when 2 then a.Fee3*b.Consumer3+a.Fee4*b.Consumer4
end) else a.FixedFee end rec,
case 
when
(case c.FeeType
when 1 then c.Fee1*b.Consumer1+c.Fee2*b.Consumer2
when 2 then c.Fee3*b.Consumer3+c.Fee4*b.Consumer4
end) > c.FixedFee then (case c.FeeType
when 1 then c.Fee1*b.Consumer1+c.Fee2*b.Consumer2
when 2 then c.Fee3*b.Consumer3+c.Fee4*b.Consumer4
end) else c.FixedFee end pay
from S_SaleManager a,S_MonthlyData b,S_Manager400 c 
where a.Number400=b.Number400 and b.Number400=c.Number400 


现在最后2个字段是应收和应付价格,我还想继续显示一个字段是应收-应付的 实际利润赚的多少
请大神帮忙,还有这个语句是否可以优化一下  
sql select

------解决方案--------------------
select Number400,_year ,_Month ,FeeType, FixedFee as a_FixedFee,FeeTypeName,Fee1,Fee2,Fee3,Fee4,
Consumer1,Consumer2,Consumer3,Consumer4, FixedFee as c_FixedFee,
case when rec>a_FixedFee then rec else a_FixedFee end as rec,
case when pay>c_FixedFee then pay else c_FixedFee end as pay,
case when rec>a_FixedFee then rec else a_FixedFee end-case when pay>c_FixedFee then pay else c_FixedFee end as profit  -->加利润字段
from 
(
select a.Number400,b._year ,b._Month ,a.FeeType, a.FixedFee as a_FixedFee,a.FeeTypeName,a.Fee1,a.Fee2,a.Fee3,a.Fee4,
b.Consumer1,b.Consumer2,b.Consumer3,b.Consumer4,c.FixedFee as c_FixedFee,
case a.FeeType
when 1 then a.Fee1*b.Consumer1+a.Fee2*b.Consumer2
when 2 then a.Fee3*b.Consumer3+a.Fee4*b.Consumer4
end as rec,
case c.FeeType
when 1 then c.Fee1*b.Consumer1+c.Fee2*b.Consumer2
when 2 then c.Fee3*b.Consumer3+c.Fee4*b.Consumer4
end as pay
from S_SaleManager a
inner join S_MonthlyData b on a.Number400=b.Number400
inner join S_Manager400 c on b.Number400=c.Number400
)t