I am busy rewriting our school administration system.
when recording attendance in the OLD database, it did NOT record the classname (FormID) against the attendance record.
We need this information for reporting purposes.
One handy table is the STUDENTA (Student Audit) table which shows me the date/time that ANY student info was changed.
From this, I am able to extract where the FORMID changed as follows.
select cstudid, formid, max(chgdate) as Dt, 0 as ID_FormID, '01/01/1999' as ChgDt
into #A
from QUAD.DBO.EBIZDW11_STUDENTA
group by Cstudid, formid
order by cstudid, Dt;
The following is the typical output of the AUDIT file.
NOTE: I have modified the data so that I don't have to upload hundreds of records.
cstudid..formid..Dt.........ID_FormID..ChgDt
564......8.4.....20130312...16.........2013-03-12
564......9.1.....20130315...2..........2013-03-15
564......GO......20130317...4..........2013-03-19
564......KINTO...20130322...3..........2013-03-28
The NEW attendance history table now contains a formid field (TYPE INT with a default value of 1) that I would like to update retrospectively.
The the relative fields look like this.
ID_Att_History...ID_Student...FormID...ATT_Date
1................564..........1........2013-03-11
2................564..........1........2013-03-12
3................564..........1........2013-03-13
4................564..........1........2013-03-14
5................564..........1........2013-03-19
6................564..........1........2013-03-20
7................564..........1........2013-03-21
8................564..........1........2013-03-22
9................564..........1........2013-03-23
Now I could update SOME of the records in the NEW table by matching on the ID_Student and CHGDAT using
Update #A
Set ID_Formid = F.ID_FORMID
from #A
Inner join SCH_FORMID F
on #A.formID = F.Fmname;
but this only changes records where the dates match so now the data looks like this.
ID_Att_History...ID_Student...FormID...ATT_Date
1................564..........1........2013-03-11
2................564..........16.......2013-03-12 FormID changes here
3................564..........1........2013-03-13
4................564..........1........2013-03-14
5................564..........1........2013-03-16
6................564..........4........2013-03-19 FormID changes here
7................564..........1........2013-03-21
8................564..........1........2013-03-22
9................564..........1........2013-03-23
So, bearing in mind that the AUDIT file reflects a change in the student record which may not correlate with a date in the ATTENDANCE record.
For example, there is no ATTENDANCE record for line 2 of the AUDIT file.
564......9.1.....20130315...2..........2013-03-15
So what I am actually trying to achieve is
Update all records where attendance exists BETWEEN the ChgDts (from the AUDIT table).
IE...the data should be changed as follows.
ID_Att_History...ID_Student...FormID...ATT_Date
1................564..........1........2013-03-11
2................564..........16.......2013-03-12 FormID changes here
3................564..........16.......2013-03-13
4................564..........16.......2013-03-14
5................564..........2........2013-03-16..FormID changes here
6................564..........3........2013-03-19 FormID changes here
7................564..........3........2013-03-21
8................564..........3........2013-03-22
9................564..........3........2013-03-23
I hope this makes sense?
Any assistance on this process would help