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


