SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


CASE Statement multiple aurgument


CASE Statement multiple aurgument

Author
Message
Sanjeev Jha-475652
Sanjeev Jha-475652
SSC Journeyman
SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)

Group: General Forum Members
Points: 78 Visits: 197
Hello,

I searched online and this forum and I am not convinced that I find the solution.

I am trying to write a TSQL statement as follows:


SELECT DISTINCT cs.SITE
, CASE WHEN cv.accepted = 1 AND cv.StepNo = 1 then 'Y'
ELSE CONVERT(varchar,[dbo].[fn_Get_WorkingDays] (ci.plannedinstalldt, -17))
FROM
dbo.Cy_SiteSurvey AS cs WITH (nolock)
INNER JOIN
dbo.Cy_Visit_Checklist AS cv WITH (nolock)
ON cv.SysSiteID = cs.SysSiteID
WHERE cv.Visit = 'V1'



This is returning two results. I am expecting only 'Y'. Is this a right way to write CASE statement? Thank you!
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16274 Visits: 19551
It is possible for a CASE block to change the number of rows returned by SELECT with DISTINCT.
Find a filter (a WHERE clause) which returns only rows which you think are affected, and test them using something like this:
SELECT --DISTINCT 
cs.SITE,

cv.accepted,
cv.StepNo,
ci.plannedinstalldt,

CASE
WHEN cv.accepted = 1 AND cv.StepNo = 1 then 'Y'
ELSE CONVERT(varchar,[dbo].[fn_Get_WorkingDays] (ci.plannedinstalldt, -17))
END
FROM dbo.Cy_SiteSurvey AS cs WITH (nolock)
INNER JOIN dbo.Cy_Visit_Checklist AS cv WITH (nolock)
ON cv.SysSiteID = cs.SysSiteID
WHERE whatever



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Sanjeev Jha-475652
Sanjeev Jha-475652
SSC Journeyman
SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)

Group: General Forum Members
Points: 78 Visits: 197
I apologize but that did not work. I am still getting two records. I may have to dig deep in my JOINS. I do appreciate you taking a look into it. Again, thank you for your time and effort. Much appreciated!!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search