SELECT IFNULL(ROUND(SUM(BillingAmount)-SUM(RecAmount)),0) OutStadningAmount FROM (SELECT IFNULL(SUM(IF(plos.`IsSampleCollected`='R',0,plos.`PCCInvoiceAmt`)),0) BillingAmount,0 RecAmount FROM `patient_labinvestigation_opd_share` plos INNER JOIN `f_ledgertransaction` lt ON lt.`LedgerTransactionID`=plos.`LedgerTransactionID` AND IFNULL(plos.`InvoiceNo`,'')='' INNER JOIN f_panel_master fpm ON fpm.`Panel_ID`=lt.`Panel_ID` WHERE fpm.`InvoiceTo`=1015 GROUP BY fpm.`InvoiceTo` UNION ALL SELECT SUM(im.ShareAmt) BillingAmount,0 RecAmount FROM invoicemaster im WHERE im.PanelID=1015 AND im.IsCancel=0 GROUP BY im.`PanelID` UNION ALL SELECT 0 BillingAmount,SUM(ReceivedAmt) RecAmount FROM invoicemaster_onaccount io WHERE io.Panel_ID=1015 AND io.IsCancel=0 AND io.Type='ON ACCOUNT' ) t