select HSN_Code as 'HSN Code', cast(Taxable_Value as decimal(10,2)) as 'Taxable Value', Vat_Rate as 'Tax Rate', cast(max(CGST) as decimal(10,2)) as CGST, cast(Max(SGST) as decimal(10,2)) SGST, cast(Max(IGST) as decimal(10,2)) IGST, cast(Taxable_Value + max(cgst) + max(sgst) + max(igst) as decimal(10,2)) as 'Total' from ( SELECT HSN_Code as 'HSN Code', abs(sum(cr_amount - dr_amount)) as Taxable_Value, Vat_Rate, ledger_id, ( case when Ledger_ID like '0x2x14xOCG%' then ABS(SUM(Credit_Amount-Debit_Amount)) else 0 end) as CGST, ( case when Ledger_ID like '0x2x14xOSG%' then ABS(SUM(Credit_Amount-Debit_Amount)) else 0 end) as SGST, ( case when Ledger_ID like '0x2x14xOIG%' then ABS(SUM(Credit_Amount-Debit_Amount)) else 0 end) as IGST From $trans_db.Transaction_Helper trans join $trans_db.transaction_item_helper itemtrans on trans.voucher_id = itemtrans.voucherID  join $master_db.Sales_Inventory_Items item on itemtrans.itemid = item.item_id  where ( (item.Vat_Rate = 5 and (Ledger_ID = '0x2x14xOCG25' or ledger_id = '0x2x14xOSG25')) OR (item.Vat_Rate = 12 and (Ledger_ID = '0x2x14xOCG6' or ledger_id = '0x2x14xOSG6')) OR (item.Vat_Rate = 18 and (Ledger_ID = '0x2x14xOCG9' or ledger_id = '0x2x14xOSG9')) OR (item.Vat_Rate = 28 and (Ledger_ID = '0x2x14xOCG14' or ledger_id = '0x2x14xOSG14')) ) and voucher_Date >=str_to_date('$fromDateTime','%Y-%m-%d %H:%i:%s') and voucher_Date <= str_to_date('$toDateTime','%Y-%m-%d %H:%i:%s') group by hsn_code, ledger_id ) A group by HSN_Code