SELECT 'L' as Ind, LEDGER_ID, LEDGER_NAME as Ledger, (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,   CAST( COALESCE(SUM(TRANS.CREDIT_AMOUNT),0) - COALESCE(SUM(TRANS.DEBIT_AMOUNT),0) AS DECIMAL(10,2)) AS CLOSING  FROM $master_db.Ledger_Master LED  INNER JOIN   (SELECT  GROUP_ID,          GROUP_NAME,          PARENT_ID   FROM    (SELECT * FROM $master_db.account_groups ORDER BY PARENT_ID, GROUP_ID) GROUPS_SORTED, (SELECT @PV := '$f1') INITIALISATION  WHERE   FIND_IN_SET(PARENT_ID, @PV)  AND     LENGTH(@PV := CONCAT(@PV, ',', GROUP_ID))  UNION   SELECT  GROUP_ID,          GROUP_NAME,          PARENT_ID   FROM    $master_db.account_groups    WHERE GROUP_ID = '$f1') AS ACC_GROUP  ON LED.GROUP_ID = ACC_GROUP.GROUP_ID  LEFT OUTER JOIN $trans_db.TRANSACTION_HELPER TRANS  ON LED.LEDGER_ID = TRANS.LEDGER_ID  AND TRANS.VOUCHER_DATE <= '$toDateTime'  GROUP BY LED.LEDGER_ID)A  WHERE CLOSING <>0