I am using a timetabling application called CELCAT and trying to pull out some data about when students should have been marked for reporting... This seems to be extremely difficult because of the way the adding and removing of students on registers is structured see below:
studentid eventid fromdatetime addition removal
25149 25145 2009-09-12 10:30:00.000 Y NULL
25149 25145 2009-09-12 10:30:00.000 NULL Y
25149 25145 2009-09-12 10:30:00.000 Y NULL
25150 23013 2009-09-08 09:00:00.000 Y NULL
25150 23554 2009-09-07 09:00:00.000 Y NULL
25150 25145 2009-09-12 10:30:00.000 Y NULL
25150 25145 2009-07-27 00:00:00.000 NULL Y
25150 25145 2009-09-12 10:30:00.000 Y NULL
25150 25145 2009-09-12 10:30:00.000 NULL Y
25150 25145 2009-09-12 10:30:00.000 Y NULL
25150 25148 2009-09-12 15:00:00.000 Y NULL
25151 25145 2009-09-12 10:30:00.000 Y NULL
25151 25145 2009-10-10 00:00:00.000 NULL Y
25152 25145 2009-09-19 10:30:00.000 Y NULL
25152 25145 2009-07-27 00:00:00.000 NULL Y
So an addition of a student means they should be marked from that date onwards in the register (registers are weekly reccurring events with their own week profile, I can handle that side of it though). A removal would mean the student doesn't need to be marked past this date, however a student could potentially be added, removed and then re-added in a later week.
What I think would get me in the right direction would be to get a table of structure
studentid eventid fromdate todate
25149 25145 2009-09-12 10:30:00.000 2009-09-28 10:30:00.000
25149 25145 2009-10-13 10:30:00.000 2009-10-24 10:30:00.000
Any ideas how to do this? Or a better suggestion? I imagine it will involve some use of cursors unless someone has an awesome solution. The tables are designed by CELCAT and cannot be modified.
Oh yeah it's sql server 2005.
EDIT by KM, here is some code to test solutions with:
DECLARE @YourTable table (studentid int
,eventid int
,fromdatetime datetime
,addition char(1)
,removal char(1)
)
SET NOCOUNT ON
INSERT INTO @YourTable VALUES (25149,25145,'2009-09-12 10:30:00.000','Y' ,NULL)
INSERT INTO @YourTable VALUES (25149,25145,'2009-09-12 10:30:00.000', NULL,'Y')
INSERT INTO @YourTable VALUES (25149,25145,'2009-09-12 10:30:00.000','Y' ,NULL)
INSERT INTO @YourTable VALUES (25150,23013,'2009-09-08 09:00:00.000','Y' ,NULL)
INSERT INTO @YourTable VALUES (25150,23554,'2009-09-07 09:00:00.000','Y' ,NULL)
INSERT INTO @YourTable VALUES (25150,25145,'2009-09-12 10:30:00.000','Y' ,NULL)
INSERT INTO @YourTable VALUES (25150,25145,'2009-07-27 00:00:00.000', NULL,'Y')
INSERT INTO @YourTable VALUES (25150,25145,'2009-09-12 10:30:00.000','Y' ,NULL)
INSERT INTO @YourTable VALUES (25150,25145,'2009-09-12 10:30:00.000', NULL,'Y')
INSERT INTO @YourTable VALUES (25150,25145,'2009-09-12 10:30:00.000','Y' ,NULL)
INSERT INTO @YourTable VALUES (25150,25148,'2009-09-12 15:00:00.000','Y' ,NULL)
INSERT INTO @YourTable VALUES (25151,25145,'2009-09-12 10:30:00.000','Y' ,NULL)
INSERT INTO @YourTable VALUES (25151,25145,'2009-10-10 00:00:00.000', NULL,'Y')
INSERT INTO @YourTable VALUES (25152,25145,'2009-09-19 10:30:00.000','Y' ,NULL)
INSERT INTO @YourTable VALUES (25152,25145,'2009-07-27 00:00:00.000', NULL,'Y')
SET NOCOUNT OFF