SELECT 'V' as Ind, VoucherId, VoucherPrefix, VoucherType, date(VOUCHERDATE) as 'Voucher Date' , cast(VOUCHERID as Integer) as 'Voucher No' , VOUCHERTYPE as 'Voucher Type' ,   CAST(SUM(CR_QTY) AS DECIMAL(10,2)) as 'Outward' , CAST(SUM(DR_QTY) AS DECIMAL(10,2)) as 'Inward'  FROM $trans_db.TRANSACTION_ITEM_HELPER WHERE ITEMID = '$f1' AND VOUCHERDATE >= str_to_date('$fromDateTime','%Y-%m-%d') AND VOUCHERDATE <= str_to_date('$toDateTime','%Y-%m-%d') AND (case when To_Location = 'OUT' then To_Ledger else From_Ledger end ) = '$f2' GROUP BY  VOUCHERTYPE, VOUCHERID, VOUCHERPREFIX  ORDER BY 1, VOUCHERTYPE, 2, VOUCHERPREFIX