SELECT
  XE.XeroEntityName AS AccountBookName,
  JL.AccountCode,
  JL.AccountName,
  JL.AccountType,
  IF(SUM(JL.NetAmount) >= 0.0, SUM(JL.NetAmount), 0.0) AS Debit,
  IF(SUM(JL.NetAmount) < 0.0, -1.0 * SUM(JL.NetAmount), 0.0) AS Credit
FROM
  taAccountBookJournal AS AJ
    LEFT JOIN taAccountBookJournalLine AS JL ON ((JL.AccountBookType = AJ.AccountBookType) AND (JL.AccountBookID = AJ.AccountBookID) AND (JL.JournalNumber = AJ.JournalNumber))
    LEFT JOIN taXeroEntity             AS XE ON (XE.XeroEntityID = AJ.AccountBookID)
WHERE
  (AJ.AccountBookType = 'Xero') AND
  (AJ.AccountBookID = @nAccountBookID) AND
  (AJ.JournalDate >= @aFromDate) AND
  (AJ.JournalDate <= @aToDate)
GROUP BY
  1,2,3,4
ORDER BY
  JL.AccountCode
