Files
Fuchs_Intranet/Fuchs_Database/dbo/Functions/fds__fn_InvoicePaymentAmounts.sql
T
2026-09-16 22:31:27 +02:00

24 lines
1.1 KiB
Transact-SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- =============================================
-- 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]
)