Solved

sql query case statement

Posted on 2014-12-30
3
253 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 69

Accepted Solution

by:
Scott Pletcher earned 500 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 65

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

680 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