Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql query case statement

Posted on 2014-12-30
3
Medium Priority
?
278 Views
Last Modified: 2014-12-30
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

0
Comment
Question by:erikTsomik
3 Comments
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 40524693
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 (
                              SELECT COUNT(1)
                              FROM sessionMap SM WITH (NOLOCK)
                              WHERE SM.sessionKey = S.sessionKey
                              ) >= IsNull(SU.btwSeatsOverride, CASE WHEN SMM.type = 'SBW' THEN 1 ELSE S.Seats END)
                     )
                  ) OR (S.productKey =1))
        )
    )
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40524695
>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.
0
 
LVL 19

Author Closing Comment

by:erikTsomik
ID: 40524986
Great. Thank you
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question