I am trying to select records based on a status 'N' or 'A' by a certain date as long as by the chosen date there is not another status given to the same course like 'D' or 'X'.
Here are some sample data...
CREATE TABLE #TEST (ID VARCHAR(7), COURSE VARCHAR(10), CURRENT_STATUS VARCHAR(1), STATUS_DATE DATETIME) INSERT INTO #TEST (ID, COURSE, CURRENT_STATUS, STATUS_DATE) VALUES ('0020541','AAAA-1000H', 'X', '2013-08-27 00:00:00.000'), ('0020541','AAAA-1000H', 'N', '2013-08-26 00:00:00.000'), ('0020541','BBBB-2000H', 'D', '2013-11-04 00:00:00.000'), ('0020541','BBBB-2000H', 'N', '2013-08-27 00:00:00.000'), ('0020541','CCCC-3000H', 'D', '2013-09-11 00:00:00.000'), ('0020541','CCCC-3000H', 'N', '2013-08-26 00:00:00.000'),
('0020541','DDDD-4000H', 'A', '2013-08-25 00:00:00.000'),
('0020541','EEEE-5000H', 'N', '2013-08-26 00:00:00.000') SELECT * FROM #TEST DROP TABLE #TEST
If the query is to determine how many records with a status of N or A up to and incl August 26, 2013, I should see 4 records... AAAA-1000H, CCCC-3000H, DDDD-4000H, EEEE-5000H
If the query is to determine how many records with a status of N or A up to and incl October 1, 2013, I should see 3 records... BBBB-2000H, DDDD-4000H, EEEE-5000H
...and so on.
Any help is appreciated.