Page 1 of 1

TVF_ActivityStaffMainFaculty

Posted: Thu Mar 02, 2017 10:34 pm
by robinwilson16
Returns the main faculty a staff member teaches in (some staff teach across multiple faculties)

Code: Select all

CREATE FUNCTION [dbo].[TVF_ActivityStaffMainFaculty] (
	@Session VARCHAR(7),
	@StaffCode VARCHAR(20)
)
RETURNS TABLE
AS
RETURN

	--DECLARE @Session VARCHAR(7) = '2015-16'

	SELECT TOP 1
		Value = FAC.d_reference
	FROM ulive.dbo.capd_staff STF
	INNER JOIN ulive.dbo.capd_staffactivity STFA
		ON STFA.sa_activitystaff = STF.s_id
	INNER JOIN ulive.dbo.capd_activity A
		ON A.a_id = STFA.sa_activity
	INNER JOIN ulive.dbo.capd_moduleactivity MA
		ON MA.ma_activity = A.a_id
	INNER JOIN ulive.dbo.capd_module GRP
		ON GRP.m_id = MA.ma_activitymodule
	INNER JOIN ulive.dbo.capd_department FAC
		ON FAC.d_id = GRP.m_moduledept
	WHERE 
		STF.s_staffreference = @StaffCode
		AND GRP.m_start <= LEFT ( @Session, 2 ) + RIGHT ( @Session, 2 ) + '-07-31'
		AND GRP.m_end >= LEFT ( @Session, 4 ) + '-08-01'​​
	GROUP BY
		FAC.d_reference
	ORDER BY
		COUNT ( FAC.d_reference ) DESC