Select
  rc.id As id,
  Account.AccountNrWithoutDots As Rekeningnummer,
  'EUR' As Valuta,
  Format(rc.CurrencyDate, 'dd-mm-yyyy') As Rentedatum,
  rc.AmountInEuro As Bedrag,
  rc.RelationAccountWithoutDots As TegenrekeningNummer,
  IIF(rb.Accounts > 1, Relation.Name + 'XXX' + rc.RelationAccountWithoutDots, Relation.Name) As TegenrekeningNaam,
  Format(rc.[Date], 'dd-mm-yyyy') As Boekdatum,
  LCase(rc.BankTransactionCode) As Betaalcode,
  rc.BankDescription As Omschrijving,
  rc.[Memo] As Opmerking,
  switch(Not IsNull([Section].Description), [Section].Description, rc.AmountInEuro > 0 And (rc.RelationAccountWithoutDots = '1'
  Or rc.RelationAccountWithoutDots = '2'), 'Van Kas', rc.AmountInEuro < 0 And (rc.RelationAccountWithoutDots = '1' Or
  rc.RelationAccountWithoutDots = '2'), 'Naar Kas', rc.AmountInEuro > 0 And (rc.RelationAccountWithoutDots = '3' Or
  rc.RelationAccountWithoutDots = '4' Or
  rc.RelationAccountWithoutDots = '5'), 'Van Chipknip', rc.AmountInEuro < 0 And (rc.RelationAccountWithoutDots = '3' Or
  rc.RelationAccountWithoutDots = '4' Or
  rc.RelationAccountWithoutDots = '5'), 'Naar Chipknip', rc.AmountInEuro > 0 And (rc.RelationAccountWithoutDots = '*******56' Or
  rc.RelationAccountWithoutDots = '*******64' Or
  rc.RelationAccountWithoutDots = '*******72'), 'Van Betaalrekening', rc.AmountInEuro < 0 And (rc.RelationAccountWithoutDots =
  '*******56' Or rc.RelationAccountWithoutDots = '*******64' Or
  rc.RelationAccountWithoutDots = '*******72'), 'Naar Betaalrekening', rc.AmountInEuro > 0 And (rc.RelationAccountWithoutDots =
  '*******714' Or
  rc.RelationAccountWithoutDots = '*******379'), 'Van Spaarrekening', rc.AmountInEuro < 0 And (rc.RelationAccountWithoutDots =
  '*******714' Or rc.RelationAccountWithoutDots = '*******379'), 'Naar Spaarrekening') As Rubriek
From
  ((((Select
    ra.FKRelation,
    Count(ra.RelationAccount) As Accounts
  From
    (Select
      [Transaction].FKRelation,
      [Transaction].RelationAccount
    From
      [Transaction]
    Where
      [Transaction].FKRelation <> 0
    Group By
      [Transaction].FKRelation, [Transaction].RelationAccount
    Order By
      [Transaction].FKRelation,
      [Transaction].RelationAccount) As ra
  Group By
    ra.FKRelation) As rb Right Join
  [Transaction] rc On rb.FKRelation = rc.FKRelation) Left Join
  Relation On rc.FKRelation = Relation.id) Inner Join
  Account On rc.FKAccount = Account.id) Left Join
  [Section] On rc.FKSection = [Section].id
Order By
  rc.id
