I have a database that tracks user's attempts at tests. I need to determine how many days have passed between each test. It should not be that hard, but I just can't seem to find the right combination needed to make it work. I have tried using ctes, sub-queries to get max dates, self-joins and I just can't get the data to match up.
I am pretty sure I could make this work by writing a cursor, but I am trying to avoid that. I basically need to find the "previous" record based on the current record. Previous in this case means the closest date to the date of the record and the Id being at least one less than the current record.
I am not asking for full code (unless you want to )...just a nudge in the right direction. JOIN, CTE, SUB-QUERY, Whatever method
Database is SQL 2008
Thank you.
=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)
Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
C#.NET Programmer
I am pretty sure I could make this work by writing a cursor, but I am trying to avoid that. I basically need to find the "previous" record based on the current record. Previous in this case means the closest date to the date of the record and the Id being at least one less than the current record.
I am not asking for full code (unless you want to )...just a nudge in the right direction. JOIN, CTE, SUB-QUERY, Whatever method
Database is SQL 2008
Thank you.
SQL:
DECLARE @StudentCourseWork TABLE
(
PersonId INT
, ProgramId INT
, TestId INT
, AttemptDate DATETIME
)
INSERT INTO @StudentCourseWork VALUES (1, 1, 1, '2013-10-11')
INSERT INTO @StudentCourseWork VALUES (1, 1, 2, '2013-10-12')
INSERT INTO @StudentCourseWork VALUES (1, 1, 3, '2013-10-14')
INSERT INTO @StudentCourseWork VALUES (1, 2, 1, '2013-10-15')
INSERT INTO @StudentCourseWork VALUES (1, 2, 2, '2013-10-20')
INSERT INTO @StudentCourseWork VALUES (2, 1, 1, '2013-10-11')
INSERT INTO @StudentCourseWork VALUES (2, 1, 2, '2013-10-13')
INSERT INTO @StudentCourseWork VALUES (2, 1, 3, '2013-10-13')
INSERT INTO @StudentCourseWork VALUES (2, 1, 4, '2013-10-14')
INSERT INTO @StudentCourseWork VALUES (3, 1, 1, '2013-10-08')
INSERT INTO @StudentCourseWork VALUES (3, 1, 2, '2013-10-09')
INSERT INTO @StudentCourseWork VALUES (3, 1, 3, '2013-10-10')
INSERT INTO @StudentCourseWork VALUES (3, 2, 1, '2013-10-13')
INSERT INTO @StudentCourseWork VALUES (3, 2, 2, '2013-10-22')
INSERT INTO @StudentCourseWork VALUES (3, 2, 3, '2013-10-26')
SELECT
PersonId
, ProgramId
, TestId
, AttemptDate
, DATEDIFF(DAY, 'previous attempt date', AttemptDate) 'DaysSincePreviousAttempt'
FROM @StudentCourseWork
/*
Expected Output
PersonId ProgramId TestId AttemptDate DaysSincePreviousAttempt
1 1 1 2013-10-11 NULL
1 1 2 2013-10-12 1
1 1 3 2013-10-14 2
1 2 1 2013-10-15 NULL
1 2 2 2013-10-20 5
2 1 1 2013-10-11 NULL
2 1 2 2013-10-13 2
2 1 3 2013-10-13 0
2 1 4 2013-10-14 1
3 1 1 2013-10-08 NULL
3 1 2 2013-10-09 1
3 1 3 2013-10-13 1
3 2 1 2013-10-13 NULL
3 2 2 2013-10-22 9
3 2 3 2013-10-26 4
*/
=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)
Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
C#.NET Programmer