TVF_ActivityStaffMainFaculty

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: 184
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_ActivityStaffMainFaculty

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

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

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests