Let's make this easy.
I have a table.
visitID studentname dob roll.no value displIndex propID
200019 rob 05/18/1937 101 smoking 2 83
200019 rob 05/18/1937 101 2 91
200019 rob 05/18/1937 101 alcohol 1 83
200019 rob 05/18/1937 101 1 91
200020 henry 08/20/1987 102 smoking 2 83
200020 henry 08/20/1987 102 2 91
200021 king 09/21/1982 103 alcohol 1 83
200021 king 09/21/1982 103 1 91
I have a table of a similar kind.
Now what I am trying to do is if there is no display index of 1 for that particular visitID
of that student, then in the case statement return a value of '779'
.
select visitID,studentname
,dob
,roll.no
,value
,dispIndex
,propID
,case
WHEN (PATINDEX('%smoking%',value) >0 OR
(PATINDEX('%sometimes smoking%',value) > 0) THEN
'777'
WHEN (PATINDEX(%not smoking%,value) >0 OR
(PATINDEX(%do not smoke%,value) >0_ THEN
'778'
WHEN (ISNULL(CAST(value as varchar(max)),'')='') THEN
'779'
ELSE
'779'
END VALUE
Here King doesn't have a record under smoking. So I want it to go into the 779 bucket. How can I perform that?