--tu celshi igulisxmeba biujetis celi

SELECT 

con.year,
sum(ISNULL(po.AMOUNT,0)-ISNULL(po.returnedamount,0))
FROM 
BUDGET_PROGRAM_COMPONENT bpc INNER  JOIN
BUDGET_PROGRAM_SUBCOMPONENT bpsc ON bpc.id=bpsc.BUDGET_PROGRAM_COMPONENT_ID INNER  JOIN	
[CONTRACT] con ON con.BUDGET_PROGRAM_SUBCOMPONENT_ID=bpsc.ID INNER JOIN 
ORGANIZATION org ON con.ORGANIZATION_ID=org.ID INNER JOIN	
PROVIDED_SERVICE ps ON ps.CONTRACT_ID=con.ID INNER JOIN
LIABILITY_DOC ld ON ld.PROVIDED_SERVICE_ID=ps.ID  INNER JOIN
LIABILITY_DOC_DETAIL ldd ON ldd.LIABILITY_DOC_ID=ld.ID  INNER JOIN
PAYMENT_ORDER po ON po.LIABILITY_DOC_DETAIL_ID=ldd.ID   INNER JOIN
COMMAND_ACCESS_LOG cal ON cal.ROW_ID=po.ID  and cal.COMMAND_ID=63 AND cal.RESULTING_STATUS_ID=po.STATUS_ID
AND CAL.ID=(SELECT MAX(ID)FROM COMMAND_ACCESS_LOG cal WHERE cal.ROW_ID=po.ID  and cal.COMMAND_ID=63 AND cal.RESULTING_STATUS_ID=po.STATUS_ID)
			
WHERE 
org.id=183 and
bpsc.id in (957,860,554) and
(cal.BUSINESS_EVENT_DATE > '2009.04.30'  AND cal.BUSINESS_EVENT_DATE < '2014.10.01') and 
ps.REPORTING_PERIOD_START>'2009.04.30' and ps.REPORTING_PERIOD_END<'2014.10.01' and
ps.status_id=14 and
po.STATUS_ID in (41)


group by con.year

-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------

--tu celshi igulisxmeba kalendaruli celi

SELECT 
year(ps.REPORTING_PERIOD_START),
sum(ISNULL(po.AMOUNT,0)-ISNULL(po.returnedamount,0)) 		
FROM 
BUDGET_PROGRAM_COMPONENT bpc INNER  JOIN
BUDGET_PROGRAM_SUBCOMPONENT bpsc ON bpc.id=bpsc.BUDGET_PROGRAM_COMPONENT_ID INNER  JOIN	
[CONTRACT] con ON con.BUDGET_PROGRAM_SUBCOMPONENT_ID=bpsc.ID INNER JOIN 
ORGANIZATION org ON con.ORGANIZATION_ID=org.ID INNER JOIN	
PROVIDED_SERVICE ps ON ps.CONTRACT_ID=con.ID INNER JOIN
LIABILITY_DOC ld ON ld.PROVIDED_SERVICE_ID=ps.ID  INNER JOIN
LIABILITY_DOC_DETAIL ldd ON ldd.LIABILITY_DOC_ID=ld.ID  INNER JOIN
PAYMENT_ORDER po ON po.LIABILITY_DOC_DETAIL_ID=ldd.ID   INNER JOIN
COMMAND_ACCESS_LOG cal ON cal.ROW_ID=po.ID  and cal.COMMAND_ID=63 AND cal.RESULTING_STATUS_ID=po.STATUS_ID
AND CAL.ID=(SELECT MAX(ID)FROM COMMAND_ACCESS_LOG cal WHERE cal.ROW_ID=po.ID  and cal.COMMAND_ID=63 AND cal.RESULTING_STATUS_ID=po.STATUS_ID)
			
WHERE 
org.id=183 and
bpsc.id in (957,860,554) and
(cal.BUSINESS_EVENT_DATE > '2009.04.30'  AND cal.BUSINESS_EVENT_DATE < '2014.10.01') and 
ps.REPORTING_PERIOD_START>'2009.04.30' and ps.REPORTING_PERIOD_END<'2014.10.01' and
ps.status_id=14 and
po.STATUS_ID in (41)


group by year(ps.REPORTING_PERIOD_START)