Postman collection
Returns Read

pixiGetItemsTopReturnReason

Returns items whose most frequent return reason occurs more often than a given threshold, together with that reason and its count.

Analyses return reasons recorded on invoice lines and lists the items that are returned repeatedly for the same reason.

For every item in Items, the procedure looks at all invoice lines (invlines joined to invoices via InvoiceRef = InvoiceKey) that reference the item (ItemRef) and have a non-NULL ReturnReason. These lines are grouped by ReturnReason, and the reason with the highest number of lines is taken as the item's top return reason.

  • An item is included only if the count of its top return reason is strictly greater than MinNrOfReturns. With the default of 2, the top reason must occur at least 3 times.
  • Items without any invoice line with a return reason are not returned.
  • ReturnReason holds the most frequent reason, NrOfReturns the number of invoice lines with that reason (not the total of all returns of the item).

The result is sorted by item name. There is no paging.

Notes

  • All tables are read with NOLOCK, so uncommitted data may appear.
  • The count is based on invoice lines, not on quantities; a line with several returned units counts once.
  • If two reasons have the same highest count, which one is shown is not deterministic (no tie-breaker in the ordering).
  • The procedure evaluates correlated subqueries for every item in Items, which can be slow on large databases.
  • The header comment of the procedure ("Checks pixi* user credentials") does not match its actual function.
  • The code does not qualify column names; from which of the tables invoices or invlines the columns ReturnReason and ItemRef come cannot be determined from the code alone.

Tables used

Items · Read invoices · Read invlines · Read
SQL object:
dbo.pipiGetItemsTopReturnReason
Updated at 2026-10-30