[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_AuditStudentApplication
Page 1 of 1

TVF_AuditStudentApplication

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

Code: Select all

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

	SELECT TOP 1
		--Value = COALESCE ( SAD.sad_value, SA.sa_status ) --SAD.sad_value
		Value = COALESCE ( SAD.sad_value, 'NAP' ) --NAP = Not Yet Applied
	FROM ulive.dbo.capd_studentapplication SA
	LEFT JOIN ulive.dbo.CAPS_SYSTEM_AUDIT_TRAIL SAT --change the left join to inner join
		ON SAT.sat_id = SA.sa_id	
		AND SAT.SAT_END <= @DateChanged
	LEFT JOIN ulive.dbo.caps_system_audit_details SAD --change the left join to inner join
		ON SAD.sad_id = SAT.SAT_AUDIT
		AND SAD.sad_property = 'status'
	WHERE	
		SA.sa_id = @StudentApplicationID
	ORDER BY
		SAT.SAT_END DESC