Page 1 of 1

SSRS_TEAM_MULTI

Posted: Mon Feb 20, 2017 6:01 pm
by robinwilson16
Lists teams for multi-select:

Code: Select all

CREATE FUNCTION [dbo].[SSRS_TEAM_MULTI] (
	@Session VARCHAR(7),
	@Fac VARCHAR(MAX)
)
RETURNS TABLE
AS
RETURN

/*DECLARE @Session VARCHAR(7) = '2016-17'
DECLARE @Fac VARCHAR(50) = 'CNEB, MWDC'
*/

SELECT
	 Code = '-1',
     Description = '-- All Teams --' 

UNION ALL

SELECT DISTINCT
	Code = LEFT ( TEAM.s_reference, CHARINDEX ( '-', TEAM.s_reference ) - 1 ),
	Description = FAC.d_reference + ' - ' + LEFT ( TEAM.s_reference, CHARINDEX ( '-', TEAM.s_reference ) - 1 ) + ' - ' + TT.TeamName
FROM ulive.dbo.capd_section TEAM
INNER JOIN ulive.dbo.capd_department FAC
	ON FAC.d_id = TEAM.s_department
INNER JOIN (
	SELECT DISTINCT
		TeamID = PRG.m_modulesection
	FROM ulive.dbo.capd_module PRG
	INNER JOIN ulive.dbo.capd_department FAC
		ON FAC.d_id = PRG.m_moduledept
	WHERE
		PRG.m_start <= LEFT ( @Session, 2 ) + RIGHT ( @Session, 2 ) + '-07-31'
		AND PRG.m_end >= LEFT ( @Session, 4 ) + '-08-01'​​
		AND PRG.m_type = 'P'
		AND PRG.m_status IN ( 'R', 'P' )
		AND 
			CASE
				WHEN @Fac LIKE '%-1%' THEN 1
				ELSE 
					CASE
						WHEN ', ' + @Fac + ',' LIKE '%, ' + FAC.d_reference + ',%' THEN 1
						ELSE 0
					END
			END = 1
) CRS ON CRS.TeamID = TEAM.s_id
LEFT JOIN GC.dbo.TeamTitles TT
	ON TT.TeamCode = LEFT ( TEAM.s_reference, CHARINDEX ( '-', TEAM.s_reference ) - 1 )
WHERE
	COALESCE ( FAC.d_start, LEFT ( @Session, 2 ) + RIGHT ( @Session, 2 ) + '-07-31' ) <= LEFT ( @Session, 2 ) + RIGHT ( @Session, 2 ) + '-07-31'
	AND COALESCE ( FAC.d_end, LEFT ( @Session, 4 ) + '-08-01'​​ ) >= LEFT ( @Session, 4 ) + '-08-01'​​
	AND COALESCE ( TEAM.s_start, LEFT ( @Session, 2 ) + RIGHT ( @Session, 2 ) + '-07-31' ) <= LEFT ( @Session, 2 ) + RIGHT ( @Session, 2 ) + '-07-31'
	AND COALESCE ( TEAM.s_end, LEFT ( @Session, 4 ) + '-08-01'​​ ) >= LEFT ( @Session, 4 ) + '-08-01'​​