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

TVF_AgeAppStart

Posted: Thu Mar 02, 2017 10:39 pm
by robinwilson16
Returns the age of a learner when the application started - ok as long as learner only has one programme-level application per year

Code: Select all

CREATE FUNCTION [dbo].[TVF_AgeAppStart] (
	@Session VARCHAR(10), 
	@StudentRef VARCHAR(20)
)
RETURNS TABLE
AS
RETURN

	--DECLARE @Session VARCHAR(7) = '2015-16'
	--DECLARE @StudentRef varchar(20) = '14026853'

	SELECT 
		Value = FLOOR ( DATEDIFF ( DAY, P.p_dob, MIN ( PRG.m_start ) ) / 365.23076923074 )
	FROM ulive.dbo.capd_module PRG
	INNER JOIN ulive.dbo.capd_moduleapplication MAPP
		ON MAPP.ma_module = PRG.m_id
	INNER JOIN ulive.dbo.capd_studentapplication SAPP
		ON SAPP.sa_id = MAPP.ma_studentapplication 
	INNER JOIN ulive.dbo.capd_student S
		ON S.s_id=SAPP.sa_student
	INNER JOIN ulive.dbo.capd_person P
		ON P.p_id = S.s_id
	WHERE
		PRG.m_type = 'P'
		AND PRG.m_start <= LEFT ( @Session, 2 ) + RIGHT ( @Session, 2 ) + '-07-31'
		AND PRG.m_end >= LEFT ( @Session, 4 ) + '-08-01'​​
		AND PRG.m_start >= LEFT ( @Session, 4 ) + '-08-01'​​
		AND S.s_studentreference = @StudentRef
	GROUP BY
		P.p_dob