TVF_AgeAppStart

The list of table valued functions used by Guildford College Reports and any anyone else wishes to share
Post Reply
User avatar
robinwilson16
Site Admin
Posts: 185
Joined: Sun Oct 16, 2016 6:46 pm
Forename: Robin
Surname: Wilson
College Name: FEA
Position: MIS Consultant
LinkedIn Profile: https://www.linkedin.com/in/robinwilson16

TVF_AgeAppStart

Post by robinwilson16 » Thu Mar 02, 2017 10:39 pm

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

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest