I have looked around a few forums for a similar question but I'm not really even sure what to search for!
I have a table with some events, client names, timestamps for the event, and a category. There are four steps in a cycle.
CREATE TABLE #events (EventID INT IDENTITY(1, 1), Client VARCHAR(10), RecordTimestamp DATETIME, Step TINYINT) INSERT INTO #events (Client, RecordTimestamp, Step) VALUES ('Client A', '01-01-2014', 1), ('Client A', '01-02-2014', 2), ('Client A', '01-03-2014', 3), ('Client A', '01-04-2014', 4), ('Client A', '01-05-2014', 1), ('Client A', '01-06-2014', 2), ('Client B', '01-04-2014', 1), ('Client B', '01-06-2014', 2) SELECT * FROM #events ORDER BY RecordTimestamp, Client
So let's assume that step 2 cannot start for a given client until step 1 completes, category 3 can't start until 2 completes, etc. If today is 1/7/2014 we could say that Client A has completed one full cycle (steps 1-4), Client A is half way through cycle 2, and Client B is half way through its first cycle.
I'm trying to identify all cycles that are in progress, meaning a step 1, 2, or 3 is present without a step 4 record (by client). A query on the data above would return step 2 of Client A and Client B, both on 1/6. I no longer care about the first cycle from Client A because another cycle has started.
The table columns can be changed if necessary. Can someone at least push me in the right direction? It may involve a CTE and/or cursor, but I'm just not sure where to start.
Thanks!