I have a table as follows:
PERSON_REF, CODE_START_DATE, CODE
where the PK is PERSON_REF
I want a query giving me those records where the CODE_START_DATE is in August 2012 together with the record with the latest CODE_START_DATE Prior to this, ie this pseudocode
SELECT PERSON_REF,
CODE_START_DATE, --S/B in August 2012
CODE,
CODE_START_DATE [PrevDate], --Latest CODE_START_DATE Prior to Column 2 in Query
CODE [PrevCode] --Code at Date of Column 4 in Query
Columns 4 and 5 might be blank, in the case where there is no record prior to the column2 date
Since you’re using SQL Server, give a try to outer apply. Here’s the query
UPDATE:
I’ve added where condition for rows in August 2012