日期:2014-05-17 浏览次数:20440 次
;WITH aaa AS
(
select Num,CostName,
sum(case CostType when 1 then RealMoney else 0 end) as RealMoney(实付金额),
sum(case CostType when 1 then ShouldMoney else 0 end) as ShouldMoney(应付金额),
sum(case CostType when 0 then RealMoney else 0 end) as RealMoney(实收金额),
sum(case CostType when 0 then ShouldMoney else 0 end) as ShouldMoney(应收金额)
from B
GROUP BY Num,CostName
)
SELECT
a.NUM,TYPE,MainNum,OperaName,CostName,RealMoney(实付金额),
ShouldMoney(应付金额),RealMoney(实收金额),ShouldMoney(应收金额)
FROM A INNER JOIN aaa AS B ON A.Num=B.Num