How to Fetch records based on date range in another table

Viewed 263

I am having issue getting the correct data out of this query.

I am trying to fetch ClientID from TableA by comparing it with TableB based on Date range in TableB

Scenario: Table A service data Table B outcome data Report Requirement – List of the Clients and the total number of clients that have a date entry in Table A but missing entry for that Client in Table B in the date range within the previous six months or in the next 45 days of that date.

Two main field for comparison in the table are ClientID & Date and I am using the below query to get those client IDs from Table A

If #temp NOT NULL
DROP TABLE #TEMP

CREATE TABLE #Temp
   (ClientID int,
    Start_dt date,
    end_dt date)

Insert into #Temp
select ClientID, 
DATEADD(DAY, -180, CAST(a.Date AS date)),
DATEADD(DAY, 45, CAST(a.Date AS date))
FROM TableA a


SELECT DISTINCT B.ClientID,B.Date
FROM Table_B b
LEFT JOIN #temp x
ON b.ClientID = x.ClientID
WHERE CAST(b.Date AS date)< x.start_dt 
or CAST(b.Date AS date)> x.end_dt 

P.S. I am using all the cast as the dates are in Varchar format as this table is created and populated from Azure.

Could be something really simple but is not striking,

Thanks heaps.

0 Answers
Related