|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2002-04-12 : 08:53:44
|
| John Smith writes "I have two tables, "activity" and "session_log". The activity table containsactivity ids and the begin and end dates of each activity. The session_logtable records each activity meeting, including the meeting date(session_DATE). Logically speaking, each session_DATE must fall between theapplicable ACT_BEGIN and ACT_END dates.CREATE TABLE [dbo].[Activity] ( [ACT_ID] [int] NOT NULL , [ACT_BEGIN] [datetime] NOT NULL , [ACT_END] [datetime] NOT NULL ,) ON [PRIMARY]CREATE TABLE [dbo].[session_log] ( [session_ID] [int] NOT NULL , [ACT_ID] [int] NOT NULL , [session_DATE] [datetime] NOT NULL ,) ON [PRIMARY]INSERT INTO [Activity]([ACT_ID], [ACT_BEGIN], [ACT_END])VALUES(1, '6/1/00', '6/30/01')INSERT INTO [Activity]([ACT_ID], [ACT_BEGIN], [ACT_END])VALUES(2, '5/1/01', '6/30/02')INSERT INTO [Activity]([ACT_ID], [ACT_BEGIN], [ACT_END])VALUES(3, '11/1/01', '11/25/01')INSERT INTO [Activity]([ACT_ID], [ACT_BEGIN], [ACT_END])VALUES(4, '7/1/00', '11/25/00')INSERT INTO [session_log]([session_ID], [ACT_ID], [session_DATE])VALUES(1, 1, '11/25/01')INSERT INTO [session_log]([session_ID], [ACT_ID], [session_DATE])VALUES(2, 1, '6/3/00')INSERT INTO [session_log]([session_ID], [ACT_ID], [session_DATE])VALUES(3, 2, '6/30/02')INSERT INTO [session_log]([session_ID], [ACT_ID], [session_DATE])VALUES(4, 2, '11/25/00')INSERT INTO [session_log]([session_ID], [ACT_ID], [session_DATE])VALUES(5, 3, '11/25/01')The code in the past did not check for out-of-bounds session_DATE values,however. A session_DATE value of '11/25/01' for an activity with anACT_BEGIN value of '6/1/00' and an ACT_END value of '6/30/01', for example,was allowed. So the session_log table contains a mix of valid and invalidsession_date values for all activities.What I would like to do is to change the invalid session_date values tovalid ones (not necessarily ACTUAL values, but least values that fallbetween the applicable ACT_BEGIN and ACT_END dates). Not sure how toimplement this in code, but I am beginning to sketch out the process:1. create a temporary table with all possible valid session_DATE values foreach activity, kind of like a calendar; so, for example, activity 3 wouldhave all possible dates between and including '11/1/01' and '11/25/01'2. either create another temporary table or modify the first temporary tableso that already used session_DATE values are marked out (only one activitysession is allowed for each day of the activity period); so, for example,'11/25/01' is unavailable for assignment when fixing activity 3'ssession_log data entry errors since '11/25/01' has already been used...3. go through the temporary table and session_log table row by row and as an out-of-bounds session_DATEvalue is found in the session_log table, change it to a valid one using thevalid session_DATE value from the temporary table. After using a value fromthe temporary table it can no longer be used, so we move on to the next rowin the temporary table...Any ideas?" |
|