书面案例陈述以再次获得结果,我需要将结果列与Job_Level列进行比较,如果Job_Level包含结果中存在的任何一个值,则它应该在新列(ExpectedOutput)中返回true或false
select Email, Job_Levels,Answer,
case when answer = 'Assessor / Trainer' then 1
when answer = 'Administrator / Coordinator/ Assistant' then 2
when answer = 'Adviser' then 3
when answer = 'Apprentice' then 4
when answer = 'Deputy / Vice Principal' then 5
when answer = 'Chief Executive / Principal' then 6
when answer = 'Technician' then 18
when answer = 'Worker / Practitioner' then 19
end as result
from TBL_CandidateInfo
inner join tal_users
on Userid = UserID_FK
inner join [ABC_Migration].[dbo].[ABCSeeker]
on email = susername
WHERE ProfileQuestion='What is your preferred job role / level?'
当前输出
Email Job_Levels Answer result
a@gmail.com 2,1,16 Assessor / Trainer 1
b@gmail.com 2,1,16 Teaching / Lecturing 16
c@yahoo.com 12,16,18 Learner Suppor 12
预期输出:
Email Job_Levels Answer result ExpectedOutput
a@gmail.com 2,1,16 Assessor / Trainer 1 True
b@gmail.com 2,1,16 Teaching / Lecturing 16 True
c@yahoo.com 16,18 Learner Suppor 12 False
答案 0 :(得分:2)
编辑: 虽然此答案已被接受,但请注意此解决方案有一些警告。有关更简单的方法,请参阅@GuidoG的答案。
我认为您正在寻找这样的东西:
SELECT Email
, Job_Levels
, Answer
, result
, CASE
WHEN Job_Levels LIKE '%' + CONVERT(VARCHAR(2), result) + '%' THEN 'true'
ELSE 'False'
END AS ExpectedOutput
FROM (
SELECT Email, Job_Levels,Answer,
case
WHEN answer = 'Assessor / Trainer' then 1
WHEN answer = 'Administrator / Coordinator/ Assistant' then 2
WHEN answer = 'Adviser' then 3
when answer = 'Apprentice' then 4
WHEN answer = 'Deputy / Vice Principal' then 5
WHEN answer = 'Chief Executive / Principal' then 6
WHEN answer = 'Technician' then 18
WHEN answer = 'Worker / Practitioner' then 19
end as result
from TBL_CandidateInfo
inner join tal_users
on Userid = UserID_FK
inner join [ABC_Migration].[dbo].[ABCSeeker]
on email = susername
WHERE ProfileQuestion='What is your preferred job role / level?'
) T
答案 1 :(得分:2)
一种选择是使用CharIndex
函数
declare @Job_Levels varchar(100) = '2,1,16'
select case when charindex('16' + ',', @Job_Levels + ',') > 0 then 'true' else 'false' end
请注意,我添加了一个额外的内容,以确保在查找1时不会得到TRUE,并且没有1而是16。
换句话说,需要Extra来确保寻找1不会对值16返回true,而只会对确切的值1返回
您的查询将如下所示
select t.Email,
t.Job_Levels,
t.Answer,
t.result,
case when charindex(t.result + ',', t.Job_Levels + ',') > 0 then 'true' else 'false' end as ExpectedOutput
from ( select Email, Job_Levels,Answer,
case when answer = 'Assessor / Trainer' then 1
when answer = 'Administrator / Coordinator/ Assistant' then 2
when answer = 'Adviser' then 3
when answer = 'Apprentice' then 4
when answer = 'Deputy / Vice Principal' then 5
when answer = 'Chief Executive / Principal' then 6
when answer = 'Technician' then 18
when answer = 'Worker / Practitioner' then 19
end as result
from TBL_CandidateInfo
inner join tal_users on Userid = UserID_FK
inner join [ABC_Migration].[dbo].[ABCSeeker] on email = susername
WHERE ProfileQuestion='What is your preferred job role / level?'
) t
答案 2 :(得分:1)
使用此。请注意,如果我们在[co]之前和之后不使用','。[Job_Levels]结果可能不正确。例如,ID 11113在LIKE%11%语句中都显示为真实结果,但113不是正确结果。但是我的查询使用的是LIKE%,11,%,并且对于,113返回false,
SELECT co.*
,CASE WHEN Convert(varchar(MAX),',' + [co].[Job_Levels] + ',') Like '%,' + Convert(varchar(MAX),[co].[result]) + ',%' THEN
1 --true
ELSE
0 --false
END as ExpectedOutput
FROM (
select Email, Job_Levels,Answer,
case when answer = 'Assessor / Trainer' then 1
when answer = 'Administrator / Coordinator/ Assistant' then 2
when answer = 'Adviser' then 3
when answer = 'Apprentice' then 4
when answer = 'Deputy / Vice Principal' then 5
when answer = 'Chief Executive / Principal' then 6
when answer = 'Technician' then 18
when answer = 'Worker / Practitioner' then 19
end as result
from TBL_CandidateInfo
inner join tal_users
on Userid = UserID_FK
inner join [ABC_Migration].[dbo].[ABCSeeker]
on email = susername
WHERE ProfileQuestion='What is your preferred job role / level?'
) co