I have a query that uses an inline view to pull a particular episode of data, that has MIN Date and then a join from main section of query to give a range of weekly dates from that point. For some reason the outer query is returning the day after date from the date dimension table but I want it to start at the Min Date for that episode.
select q.PersonID , dd.date AS [Date] , q.min_date , q.Data1 , q.data2 FROM ( SELECT T1.P_ID , T1.Data1 , t2.Data2 , MIN(T1.WeekComm2) min_date FROM table1 T1 INNER JOIN table2 T2 ON T1.ID = T2.ID group by T1.P_ID, T1.Data1, T2.Data2 ) q CROSS JOIN DateDimension dd INNER JOIN TablePerson tp ON tp.ID = q.P_ID WHERE dd.date BETWEEN q.min_date AND GETDATE() --Output Person Date Min_Date Data1 Data2 ====== ========== ========== ===== ====== 1001 2020-03-10 2020-03-09 x y 1001 2020-03-17 2020-03-09 x y
Should be like this:
Person Date Min_Date Data1 Data2 ====== ========== ========== ===== ====== 1001 2020-03-09 2020-03-09 x y 1001 2020-03-16 2020-03-09 x y
So the Date should be the same as Min-Date to start, it will iterate one week at a time as the Min-Date is one week apart for each episode, so determining every week start date is done, but is one day ahead of the actual start date.
Min_date is a varchar if that might be the issue, have tried cast as date but makes so difference.
When I look at the full range of dates in my query it starts one day later than min date and finishes one day early from today's date.
Any ideas how to fix?