select a.element, a.subelement, a.elementnum, a.Lead, a.TotalCost, a.OtherFunds, a.Budget, a.Period, >>>convert(integer,SUM(l.amount)/1000) as expended >>>from RTA_Elements a >>>left join TIP_Projects b on a.ElementNum = convert(integer,substring(b.RtaID,5,2)) >>>left join TIP_Ledger l on b.TipID = l.tipid and l.Fundtype = 'RTA' >>>group by a.element, a.subelement, a.elementnum, a.Lead, a.TotalCost, a.OtherFunds, a.Budget, a.Period >>>order by elementnum>>>
>>>select a.element, a.subelement, a.elementnum, a.Lead, a.TotalCost, a.OtherFunds, a.Budget, a.Period, >>>convert(integer,SUM(l.amount)/1000) as expended, sum(p.amount) as programmed >>>from RTA_Elements a >>>left join TIP_Projects b on a.ElementNum = convert(integer,substring(b.RtaID,5,2)) >>>left join TIP_Ledger l on b.TipID = l.tipid and l.Fundtype = 'RTA' >>>left join TIP_ApprovedFunding p ON b.TipID = p.TipID and p.Fundtype = 'RTA' >>>group by a.element, a.subelement, a.elementnum, a.Lead, a.TotalCost, a.OtherFunds, a.Budget, a.Period >>>order by elementnumthe values for expended (which were correct before) and the values for programmed, come out wrong.
select a.element, a.subelement, a.elementnum, a.Lead, a.TotalCost, a.OtherFunds, a.Budget, a.Period, convert(integer,l.amount/1000) as expended, p.amount as programmed from RTA_Elements a left join TIP_Projects b on a.ElementNum = convert(integer,substring(b.RtaID,5,2)) left join (SELECT tipid, SUM(Ammount) AS Ammount FROM TIP_Ledger WHERE Fundtype = 'RTA' GROUP BY TipId) l on b.TipID = l.tipid left join (SELECT tipid, SUM(Ammount) AS Ammount FROM TIP_ApprovedFunding WHERE Fundtype = 'RTA' GROUP BY TipId) p on b.TipID = p.tipid order by a.elementnumThis is NOT TESTED.