Trouble with Subqueries, am I going mad.
The problem that faces me relates to the statement below:
select (isnull(c.FIRSTNAMES,'')
+ ' '
+ isnull(c.[NAME],'')) ClientName,
c.REFER ClientRef,
p.CONT_NUM PolicyRef,
p.POL_NUM PolicyID,
cs.[Desc] ,
p.STATUS
from S_POLMAI p
inner join S_CLIENT c
on p.CLIENT_NUM = c.CLIENT_NUM
inner join Hydra.dbo.def_PolicyStatus cs
on p.STATUS = cs.[ID]
where p.PLAN_CODE = 'DGT'
and p.POL_NUM not in (select [id] from #temp1) -- Line in dispute
and p.POL_NUM not in (select distinct PolicyID
from TrackItems
where [Description] like '%blar blar blar%'
or [Description] like '%blar blar blar%')
and p.STATUS in (19,20)
and p.POL_NUM = ***** -- Test Case
order by c.[NAME]
With the 'Line in dispute' commented out the statement returns a single row relating to the POL_NUM entered as a test case. The table #temp1 contains a list of POL_NUM's excluding the one I have used as my test case. POL_NUM and [id] (returned by #temp1) are of the same data type. Running the full statement returns no rows. Changing the 'Line in dispute' to 'in' rather than 'not in', also returns no rows. Replacing the select statement with a comma delimited list of numbers excluding the test case POL_NUM, works as expected.
|