Avatar of erikTsomik
erikTsomikFlag for United States of America

asked on 

sql query case statement

i have a query and want to do the case statement in the where clause but I get the error message. Basically I want say if the type is SBW then compare to 1 otherwise comapre to whatever is in the dataabse

SELECT 

distinct U.userKey
	, U.firstName
	, U.lastName
	, S.sessionKey
	, CAST(CAST(MONTH(SU.sessionStart) AS VARCHAR) + '/' + CAST(DAY(SU.sessionStart) AS VARCHAR) + '/' + CAST(YEAR(SU.sessionStart) AS VARCHAR) AS DATETIME) AS sessionDt
	, SU.sessionStart
	, SU.sessionEnd
	, SU.sessionUnitKey
	, L.locationKey
	, L.name AS locationName
	, LPT.productTypeCode
	,LPT.title
	, CASE WHEN SU.unit IS NULL
		THEN
			LPT.description
		ELSE
			'Class ' + SU.unit
		END AS sessionType

	

		FROM users U WITH (NOLOCK)
	INNER JOIN sessionUnit SU WITH (NOLOCK) ON U.userKey = SU.instructorKey
	INNER JOIN session S WITH (NOLOCK) ON SU.sessionKey = S.sessionKey
	LEFT OUTER JOIN sessionMap SMM WITH (NOLOCK) on SMM.sessionKey = S.sessionKey
	INNER JOIN product P WITH (NOLOCK) ON S.productKey = P.productKey
	INNER JOIN lkup_productType LPT WITH (NOLOCK) ON P.productTypeKey = LPT.productTypeKey
	INNER JOIN location L WITH (NOLOCK) ON S.locationKey = L.locationKey
WHERE SU.sessionStart BETWEEN '12/1/2014' AND '12/31/2014'
AND (
        S.status = 'reserved'
        OR (
            S.status = 'enabled'
            AND (
				S.productKey != 1
				AND (
				 CASE WHEN SMM.type = 'SBW' 
					THEN 
						(
							SELECT COUNT(1)
							FROM sessionMap SM WITH (NOLOCK) 
							WHERE SM.sessionKey = S.sessionKey 
						) > = IsNull(SU.btwSeatsOverride, 1)
					ELSE 
						(
							SELECT COUNT(1)
							FROM sessionMap SM WITH (NOLOCK) 
							WHERE SM.sessionKey = S.sessionKey 
							) > = IsNull(SU.btwSeatsOverride, S.Seats)
					END 
			)) OR (S.productKey =1)

			
        )

    ) 

Open in new window

Microsoft SQL ServerMicrosoft SQL Server 2008SQL

Avatar of undefined
Last Comment
erikTsomik
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

>Basically I want say if the type is SBW then compare to 1 otherwise comapre to whatever is in the dataabse
Define 'to whatever is in the database' better.
Avatar of erikTsomik
erikTsomik
Flag of United States of America image

ASKER

Great. Thank you
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo