IF EXISTS (SELECT * FROM sys.objects WHERE name = 'LD0FGetMaxEndTimeForCalculationAPI')
BEGIN
    DROP FUNCTION LD0FGetMaxEndTimeForCalculationAPI
END
GO
CREATE FUNCTION LD0FGetMaxEndTimeForCalculationAPI (
    @startDate DATETIME2,
    @endDate DATETIME2
)
RETURNS TABLE 
AS 
RETURN
(
    SELECT 
        BERAKNR, 
        MAX(ENDTIME) AS MaxEndTime
    FROM 
       LD0P0D 
    FOR SYSTEM_TIME CONTAINED IN (@startDate, @endDate)
    WHERE 
        ENDTIME BETWEEN @startDate AND @endDate
    GROUP BY 
        BERAKNR
);

GO


