SELECT 'V' as Ind, subq.Voucher_ID, subq.Voucher_Prefix, subq.Voucher_Type, subq.Ledger_ID , (SELECT ledger_name FROM $master_db.Ledger_Master where ledger_id = subq.Ledger_ID) AS Ledger, subq.Voucher_Date 'Voucher Date', subq.Voucher_ID 'Voucher No', subq.Voucher_Type as 'Voucher Type', case when subq.deb>0 then subq.deb else 0 end as Debit, case when subq.cred>0 then subq.cred else 0 end as Credit FROM ( SELECT Ledger_ID, Credit_Amount-Debit_Amount as cred, Debit_Amount-Credit_Amount as deb, Voucher_Date, Voucher_ID, Voucher_Prefix, Voucher_Type, MIN(_id) From $trans_db.Transaction_Helper Where Voucher_Date >=str_to_date('$fromDateTime','%Y-%m-%d') AND Voucher_Date <=str_to_date('$toDateTime','%Y-%m-%d') GROUP BY Voucher_ID, Voucher_Type ORDER BY Voucher_Date, Cast(Voucher_ID as unsigned), Voucher_Type ) subq