[phpBB Debug] PHP Warning: in file [ROOT]/phpbb/session.php on line 580: sizeof(): Parameter must be an array or an object that implements Countable
[phpBB Debug] PHP Warning: in file [ROOT]/phpbb/session.php on line 636: sizeof(): Parameter must be an array or an object that implements Countable
Unofficial UNIT-e Forum • TVF_AuditStudentEnrolment
Page 1 of 1

TVF_AuditStudentEnrolment

Posted: Thu Mar 02, 2017 10:45 pm
by robinwilson16
Returns the status of an enrolment at a certain point in time. Useful for accurately reporting on the number of enrolments on a certain date. Use with caution on large queries as will show your query down - running for all enrolments here takes about 2.5mins

Code: Select all

CREATE FUNCTION [dbo].[TVF_AuditStudentEnrolment] (
	@Session VARCHAR(7), 
	@StudentEnrolmentID numeric(16, 0),
	@DateChanged DateTime
)
RETURNS TABLE
AS
RETURN 
	/*
	DECLARE @Session NVARCHAR(7) = '2015-16'
	DECLARE @StudentEnrolmentID numeric(16, 0) = 727000003893609
	DECLARE @Property VARCHAR(20) = 'status'
	DECLARE @DateChanged DateTime = '2016-06-01'
	*/
	--6

	SELECT TOP 1
		Value = SAD.sad_value --COALESCE ( SAD.sad_value, ME.e_status )
	FROM ulive.dbo.capd_moduleenrolment ME
	INNER JOIN ulive.dbo.CAPS_SYSTEM_AUDIT_TRAIL SAT --changed left to inner
		ON SAT.sat_id = ME.e_id	
		AND SAT.SAT_END <= @DateChanged
	INNER JOIN ulive.dbo.caps_system_audit_details SAD --changed left to inner
		ON SAD.sad_id = SAT.SAT_AUDIT
		AND SAD.sad_property = 'status'
	WHERE	
		ME.e_id	= @StudentEnrolmentID
	ORDER BY
		SAT.SAT_END DESC