Without table structures, sample data and desired output, analyzing this query is very difficult. However, a few things do jump out at me:In the main query SELECT clause..., CASE WHEN b.nbk_id IS NULL THEN NULL ELSE b.nbk_id END AS nbk_id
this statement is confusing and seems unnecessaryIn the join...AND CONVERT(varchar(11), a.datestamp, 120) >= CONVERT(varchar(11), b.effective_date, 120)
converting them to character datatypes and then evaluating them seems to be extra work. I believe datetime fields evaluate correctly compared against eachother (meaning 10/3/2003 10:00AM is less than 10/3/2003 10:01AM)In the subquery...SELECT DISTINCT MAX(y.effective_date)
DISTINCT and an aggregate like MAX() is rarely necessary but with those joins and such it may be.In the main query...WHERE a.datestamp BETWEEN CONVERT(varchar(11),'10/1/2003',120) AND CONVERT(varchar(11),'10/1/2003',120)
Would this bring back anything? It seems there is no time gap to evaluate. Does the query bring back any results?However, with sample data and table build statements, a better analysis could be made