sql server - TSQL Case statement that checks for non matches on two columns doesn't work on second column -
this baffling me. i'm trying use case statement create column returns either 1 or 0 depending on whether search of 2 columns hospital site/ward not in list of values in table/variable, , if 2 columns represent if journey said hospital site/ward cancelled whatever reason. counting column called [journeys other] represents ambulance journeys took place anywhere other 1 of 4 other specific sites. why case statement below asking check hospitals , sites aren't in predefined list.
the problem is; query not doing ask second column; [from hospital text]. it's returning 1's in counting column there match entry in pre-defined list. works fine first column [requestee name] there no matches. second column search might not exist. it's driving me mad.
i've plucked out of it's bigger main query show i've done far;
select [journey id] , [start date] , [requestee name] , [from hospital text] , [abort reason text] , [cancel reason text] , case when [requestee name] not in (isnull(mts.matchingsite, '')) , ([from hospital text] not in (isnull(mts.matchingsite, '')) or [from hospital text] null) , coalesce([abort reason text], [cancel reason text]) null 1 else 0 end [journeys other] ae.dbo.pts_mdsforreporting1415 mds left join @matchesreq mts on mds.[requestee name] = mts.matchingsitereq [ecr flag]='0' , [service type text] = 'pallative day' , [ccg] in ('leeds north ccg','leeds south , east ccg','leeds west ccg') , [start date] >= '01-july-2014'
so when run this, [journeys other] column created case statement has 1 against rows should not qualify it.
here example rows;
journey id start date requestee name hospital text abort reason text cancel reason text journeys other 6630679 2014-07-08 00:00:00.000 dales ward st gemmas hospice null null 1 6633083 2014-07-10 00:00:00.000 moors ward st gemmas hospice null null 1 6644723 2014-07-12 00:00:00.000 j84 thoracic's st james hospital leeds null null 1 6676438 2014-07-22 00:00:00.000 day unit wheatfields hospice null null 1 6675840 2014-07-22 00:00:00.000 discharge wheatfields hospice null null 1 6694026 2014-07-28 00:00:00.000 dales ward st gemmas hospice null null 1 6693591 2014-07-28 00:00:00.000 day unit bexley wing (st james) null null 1 6693590 2014-07-28 00:00:00.000 day unit wheatfields hospice null null 1 6695586 2014-07-28 00:00:00.000 day unit wheatfields hospice null null 1
all these rows have hospital/site in [from hospital text] in list of hospitals/sites in table variable. there match column, should flagged 0 per else condition of case statement.
if it's important; there lot more values in table variable have demonstrated in few rows of results, , of them picked query , marked 0 there's match, seems hit , miss. correct rows match occurs in [requestee name] , 2nd , condition either true or not (is null or not null when coalesced together.) minute has consider value in both columns, doesn't work 2nd of two.
i cannot life of me figure out why case statement doesn't behave expected when it's comparing table variable [from hospital text] column. advice appreciated!!
Comments
Post a Comment