SQL Conditional Statement in Where Clause - sql-server-2008

I would like to write the following IF statement in SQL. Most of the where clause is constructed, but it's the condition around the statement that I'm struggling with:
if #StuYear = 11 then
AND (#TeachingGroup = 'Select All')
AND ([DataCollection] = #DataCollection)
AND ([Name] = #SubjectName)
AND (#Subgroup='Select All')
AND '' = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND Result Not Like '[ABU]%' AND Result <> ''
else if #StuYear = 10 then
AND #TeachingGroup Not Like 'Select All'
AND ([DataCollection] = #DataCollection)
AND ([Name] = #SubjectName)
AND ([TeachingGroup] = #TeachingGroup)
AND #Subgroup='Select All'
AND '' = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND Result Not Like '[ABC]%' AND Result <> ''
end if

Simply replace if with where and else if with or and surround the other conditions in paranthesis:
...
where (#StuYear = 11
AND (#TeachingGroup = 'Select All')
AND ([DataCollection] = #DataCollection)
AND ([Name] = #SubjectName)
AND (#Subgroup='Select All')
AND '' = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND Result Not Like 'A*'
AND Result Not Like 'A'
AND Result Not Like 'B'
AND Result Not Like 'U'
AND Result Not Like '' )
OR (#StuYear = 10
AND #TeachingGroup Not Like 'Select All'
AND ([DataCollection] = #DataCollection)
AND ([Name] = #SubjectName)
AND ([TeachingGroup] = #TeachingGroup)
AND #Subgroup='Select All'
AND '' = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND Result Not Like 'A*'
AND Result Not Like 'A'
AND Result Not Like 'B'
AND Result Not Like 'C'
AND Result Not Like '')

Tim's answer helped point me in the right direction. However, I eventually resolved the issue by applying the year condition within the where clause as opposed to rapping it around them:
where
(#StuYear = [stuyear]
AND #TeachingGroup = 'Select All'
AND [DataCollection] = #DataCollection
AND [Name] = #SubjectName
AND #Subgroup='Select All'
AND '' = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND ((Result Not Like '[ABU]%' and result not like '' and stuyear='11')
or(Result Not Like '[ABC]%' and result not like '' and stuyear='10')
or(Result Not Like '[ABCD]%' and stuyear='9'))
AND Result <> '')
OR
(#StuYear = [stuyear]
AND #TeachingGroup Not Like 'Select All'
AND [DataCollection] = #DataCollection
AND [Name] = #SubjectName
AND [TeachingGroup] = #TeachingGroup
AND #Subgroup='Select All'
AND '' = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND ((Result Not Like '[ABU]%' and result not like '' and stuyear='11')
or(Result Not Like '[ABC]%' and result not like '' and stuyear='10')
or(Result Not Like '[ABCD]%' and stuyear='9'))
AND Result <> '')

Related

How to multiple rows to column & show one row

I am facing a problem with the MySQL query result.
I want result rows to a column & get only one row
please suggest & help to solve my problem
I Done that in numeric values using sum() but in this case, data is string not numeric
SELECT
CASE WHEN IB.`IndicatorId` = 459 THEN AA.`Answer` ELSE '' END AS 'FO',
CASE WHEN IB.`IndicatorId` = 462 THEN AA.`Answer` ELSE '' END AS 'Main_Canal',
CASE WHEN IB.`IndicatorId` = 461 THEN AA.`Answer` ELSE '' END AS 'Parent_Canal',
CASE WHEN IB.`IndicatorId` = 473 THEN AA.`Answer` ELSE '' END AS 'Channel_length',
CASE WHEN IB.`IndicatorId` = 472 THEN AA.`Answer` ELSE '' END AS 'Design_Discharge_Cusecs',
CASE WHEN IB.`IndicatorId` = 474 THEN AA.`Answer` ELSE '' END AS 'GCA',
CASE WHEN IB.`IndicatorId` = 475 THEN AA.`Answer` ELSE '' END AS 'CCA',
CASE WHEN IB.`IndicatorId` = 471 THEN AA.`Answer` ELSE '' END AS 'Water_Courses',
A.`DataCollectorId`
FROM `answer_water_discharge` AS A
INNER JOIN `answers_answer_water_discharge` AS AA ON AA.`AnswerId` = A.`AnswerId`
INNER JOIN `activity_sections_indicators` AS ASI ON ASI.`SectionIndicatorId` = AA.`SectionIndicatorId`
INNER JOIN `indicators_bank` AS IB ON IB.`IndicatorId` = ASI.`IndicatorId`
WHERE 1=1 AND A.AnswerId = 57
I want one row with rows to column

sql two order by give different result

I have this sql query below that has 2 order by my dates are
02/01/2016 - 03/01/2017
02/03/2011 - 07/07/2016
12/22/2010 - 07/07/2016
02/01/2016 - 02/01/2016
12/22/2010 - 07/07/2013
This is the result when i use the query below. But the problem is it is not in order in what i expect for. I want to order first the DateEnd in desc order and then the DateStarted.
Select top 5
Case When dbo.VoluntaryWork.Organization + ' - ' + dbo.VoluntaryWork.OrganizationAddress = ' - '
Then 'N/A'
Else dbo.VoluntaryWork.Organization + ' - ' + dbo.VoluntaryWork.OrganizationAddress end AS OrgAddress,
Case When isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateStarted, 101),'') = '' or isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateStarted, 101),'') = '01/01/1900' then 'N/A' else isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateStarted, 101),'') end AS DateStarted,
Case When isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateEnded, 101),'') = '' or isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateEnded, 101),'') = '01/01/1900' then 'N/A' else isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateEnded, 101),'') end AS DateEnded
From dbo.PersonVoluntaryWork
Inner Join dbo.VoluntaryWork ON dbo.PersonVoluntaryWork.VoluntaryWorksId = dbo.VoluntaryWork.VoluntaryWorksId
Where (dbo.PersonVoluntaryWork.PersonId = #PersonId)
Order By dbo.VoluntaryWork.DateEnded desc ,dbo.VoluntaryWork.DateStarted desc
Hi use another select clause from outside your main query and then apply order by clause like this.. I hope this will work.
SELECT * from (
SELECT top 5 case when dbo.VoluntaryWork.Organization + ' - ' + dbo.VoluntaryWork.OrganizationAddress = ' - ' then 'N/A'
else dbo.VoluntaryWork.Organization + ' - ' + dbo.VoluntaryWork.OrganizationAddress end AS OrgAddress,
case when isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateStarted, 101),'') = '' or isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateStarted, 101),'') = '01/01/1900' then 'N/A' else isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateStarted, 101),'') end AS DateStarted,
case when isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateEnded, 101),'') = '' or isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateEnded, 101),'') = '01/01/1900' then 'N/A' else isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateEnded, 101),'') end AS DateEnded,
FROM dbo.PersonVoluntaryWork INNER JOIN
dbo.VoluntaryWork ON dbo.PersonVoluntaryWork.VoluntaryWorksId = dbo.VoluntaryWork.VoluntaryWorksId
WHERE (dbo.PersonVoluntaryWork.PersonId = #PersonId)
ORDER BY dbo.VoluntaryWork.DateEnded DESC
) A ORDER BY A.DateStarted

not retrieving exact result from case statement

I am using below query but Y column not retrieving exact result. Could you guys please help me out
SELECT FAPI.*,
CASE WHEN (SELECT DISTINCT 'Y'
FROM FLOOR_PI FAPI,
NUATON NUMF
WHERE FAPI.BRCH = NUMF.BRCH
AND FAPI.BASE = NUMF.BASE
AND FAPI.NUM = 0
AND NVL(FAPI.RATION, 'X') <> 'D'
AND FAPI.CODE = 'A'
AND NUMF.R_DATE
BETWEEN FAPI.EFF_DATE
AND FAPI.EXP_DATE ) = 'Y'
THEN 'Y'
ELSE 'N' END NEW
FROM FLOOR_PI FAPI
Thank you,
Rave
Based on #SlimsGohst comment:
You should replace FAPIx with FAPI1 or FAPI2 whichever is applicable
SELECT FAPI1.*,
CASE WHEN (SELECT DISTINCT 'Y'
FROM FLOOR_PI FAPI2,
NUATON NUMF
WHERE FAPIx.BRCH = NUMF.BRCH
AND FAPIx.BASE = NUMF.BASE
AND FAPIx.NUM = 0
AND NVL(FAPIx.RATION, 'X') <> 'D'
AND FAPIx.CODE = 'A'
AND NUMF.R_DATE
BETWEEN FAPIx.EFF_DATE
AND FAPIx.EXP_DATE ) = 'Y'
THEN 'Y'
ELSE 'N' END NEW
FROM FLOOR_PI FAPI1

MySQL IF AND OR

I have created this query:
Where
(companies.col_335 <> NULL) And
(companies.col_285 = '') Or
(companies.col_346 <> 'Yes') Or
(companies1.col_275 = '') Or
(companies1.col_294 <> 'Yes')
I want the selection to only include records where the FIRST comparision above is true, and at least one of the other 5 comparisions are true.
Is this the right way of doing this?
No. AND has higher precedence over OR (basic boolean logic). Use parentheses to do it right:
Where
(companies.col_335 <> NULL) And
((companies.col_285 = '') Or
(companies.col_346 <> 'Yes') Or
(companies1.col_275 = '') Or
(companies1.col_294 <> 'Yes'))
You are missing some brackets:
Where
(companies.col_335 <> NULL) And
(companies.col_285 = '' Or
companies.col_346 <> 'Yes' Or
companies1.col_275 = '' Or
companies1.col_294 <> 'Yes')
What you were trying to do would do the AND FIRST and then do OR with any of the last 3. i e:
(companies.col_335 <> NULL And companies.col_285 = '')
Or
companies.col_346 <> 'Yes'
Or
companies1.col_275 = ''
Or
companies1.col_294 <> 'Yes'
You're almost there... you should surround at paranthesis the or section.
WHERE
(companies.col_335 <> NULL)
AND
(
(companies.col_285 = '') Or
(companies.col_346 <> 'Yes') Or
(companies1.col_275 = '') Or
(companies1.col_294 <> 'Yes')
)

sql Alias Column Name for Use in CASE Statement

I have an sql query as below,
select Site,DataSource,
(SELECT CASE
WHEN DataSource = 'RFQ' THEN 'ChangeOperator'
ELSE 'SameOperator'
END) AS OperatorScenario,
(SELECT CASE
WHEN OperatorScenario = 'ChangeOperator' THEN '1'
ELSE '022'
END) AS OperatorScenario2
from tablename
when i execute this query i am getting exception as Invalid column name OperatorScenario. So i tried to use single codes in alias name in second query as below,
(SELECT CASE
WHEN 'OperatorScenario' = 'ChangeOperator' THEN '1'
ELSE '022'
END) AS OperatorScenario2
So then it executes always else part. Please give me some suggesssions.
Regards
sangeetha
The main issue is you cannot reference an alias in the same query. Also you have parentheses and an extra SELECT keyword, making what looks like a subquery where I don't believe you intended one (you do not have a FROM clause).
You can either copy the whole thing again (fixing your query in the process):
SELECT Site, DataSource,
CASE
WHEN DataSource = 'RFQ' THEN 'ChangeOperator'
ELSE 'SameOperator'
END AS OperatorScenario,
CASE
WHEN
CASE
WHEN DataSource = 'RFQ' THEN 'ChangeOperator'
ELSE 'SameOperator'
END = 'ChangeOperator' THEN '1'
ELSE '022'
END AS OperatorScenario2
from tablename
Or, simplifying:
SELECT Site, DataSource,
CASE
WHEN DataSource = 'RFQ' THEN 'ChangeOperator'
ELSE 'SameOperator'
END AS OperatorScenario,
CASE
WHEN DataSource = 'RFQ' THEN '1'
ELSE '022'
END AS OperatorScenario2
from tablename
Or use a CTE (again fixing your query):
WITH cte AS
(
SELECT Site, DataSource,
CASE
WHEN DataSource = 'RFQ' THEN 'ChangeOperator'
ELSE 'SameOperator'
END AS OperatorScenario,
FROM tablename
)
SELECT Site, DataSource, OperatorScenario,
CASE
WHEN OperatorScenario = 'ChangeOperator' THEN '1'
ELSE '022'
END AS OperatorScenario2
FROM cte
You cannot access the alias on the same level in this query, either use a sub-query or a cte:
WITH cte
AS (SELECT site,
datasource,
CASE
WHEN datasource = 'RFQ' THEN 'ChangeOperator'
ELSE 'SameOperator'
END AS OperatorScenario
FROM dbo.tablename)
SELECT site,
datasource,
operatorscenario,
CASE
WHEN operatorscenario = 'ChangeOperator' THEN '1'
ELSE '022'
END AS OperatorScenario2
FROM cte
Note that i've also removed the extra parantheses and select in the CASE.
I set my aliases at the beginning like this:
SELECT [ALIAS] = CASE....
Yours would be:
SELECT Site, DataSource,
[OperatorScenario] = CASE
WHEN DataSource = 'RFQ' THEN 'ChangeOperator' ELSE 'SameOperator'
END,
[OperatorScenario2] = CASE
WHEN DataSource = 'RFQ' THEN '1' ELSE '022'
END
from tablename