SELECT 'L' as Ind, LEDGER_ID, LEDGER_NAME as 'Ledger Name', (CASE WHEN CLOSING < 0 THEN -1*CLOSING  ELSE NULL END) DEBIT,   (CASE WHEN CLOSING > 0 THEN CLOSING  ELSE NULL END) CREDIT FROM  (SELECT LED.LEDGER_NAME, LED.LEDGER_ID, LED.LEDGER_TYPE, LED.GROUP_ID,  CAST(COALESCE(LED.OPENING_BALANCE,0)+  ROUND(COALESCE(SUM(TRANS.CREDIT_AMOUNT),0),2) - ROUND(COALESCE(SUM(TRANS.DEBIT_AMOUNT),0),2)  AS DECIMAL(10,2)) AS CLOSING FROM $master_db.Ledger_Master LED  LEFT OUTER JOIN $trans_db.TRANSACTION_HELPER TRANS  ON LED.LEDGER_ID = TRANS.LEDGER_ID  AND TRANS.VOUCHER_DATE <= str_to_date('$toDateTime','%Y-%m-%d %H:%i:%s')  GROUP BY LED.LEDGER_ID)A  WHERE CLOSING <>0 ORDER BY LEDGER_TYPE,GROUP_ID