代码
select KMID,RelatedCode,RelatedID,ReceivableType,QtyTD,ReceivableDate,ReceivableEndDate from vw_chg_Receivable
select newid() as KMID,c0.RelatedCode,c0.RelatedID,c0.QtyTD,c0.ReceivableDate,c0.ReceivableEndDate,max(c1.Amount) as '仓储费',max(c2.Amount) as '码头费',max(c3.Amount) '操作费'
from (select * from vw_chg_Receivable) as c0
left join (select * from vw_chg_Receivable where Goodsid=(select kmid from (select *,row=row_number() over (order by TypeName) from vw_chg_CostType)ta where row=1)) as c1 on c0.ReceivableDate=c1.ReceivableDate
left join (select * from vw_chg_Receivable where Goodsid=(select kmid from (select *,row=row_number() over (order by TypeName) from vw_chg_CostType)ta where row=2)) as c2 on c0.ReceivableDate=c2.ReceivableDate
left join (select * from vw_chg_Receivable where Goodsid=(select kmid from (select *,row=row_number() over (order by TypeName) from vw_chg_CostType)ta where row=3)) as c3 on c0.ReceivableDate=c3.ReceivableDate
group by c0.ReceivableDate,c0.ReceivableEndDate,c0.RelatedCode,c0.RelatedID,c0.QtyTD
order by c0.ReceivableDate
执行结果如下图: