-- ============================================= -- Description: Set-based counterpart of the scalar [fds__fn_InvoicePaymentAmount]: -- returns the summed (non-storno) banking payments for EVERY invoice in one pass -- over [fds__fn_bankingtransactions]. Join it instead of calling the scalar -- function per row — the scalar one declares a table variable, so SQL Server -- cannot inline it and re-runs the whole banking TVF for each invoice -- (~0.5 s × N rows; the overdue-sum widget timed out at 90 s with 187 rows). -- Same semantics as the scalar version with an empty id list: all invoices, -- no date filter, storno credits NOT included (see *_full for those). -- ============================================= CREATE FUNCTION [dbo].[fds__fn_InvoicePaymentAmounts] ( ) RETURNS TABLE AS RETURN ( SELECT [InvId] = bs.[InvId] ,[Amount] = CAST(SUM(ISNULL(bs.[Amount], 0.0)) as numeric(9,2)) FROM [dbo].[fds__fn_bankingtransactions](NULL, NULL, NULL, NULL, DEFAULT) as bs WHERE bs.[InvId] IS NOT NULL GROUP BY bs.[InvId] )