I could use some assistance in making a stored procedure which updates the contents of one table based on a second table:
Table 1 contains the following
ID Date Status
1677 2007-05-01 1
1677 2007-05-02 1
1677 2007-05-03 1
1677 2007-05-04 1
1677 2007-05-05 1
1677 2007-05-06 1
1677 2007-05-07 1
1677 2007-05-08 1
1677 2007-05-09 1
1677 2007-05-10 1
1677 2007-05-11 1
1677 2007-05-12 1
1677 2007-05-13 1
1677 2007-05-14 1
1677 2007-05-15 1
1677 2007-05-16 1
1677 2007-05-17 1
1677 2007-05-18 1
1677 2007-05-19 1
1677 2007-05-20 1
1677 2007-05-21 1
1677 2007-05-22 1
1677 2007-05-23 1
1677 2007-05-24 1
1677 2007-05-25 1
1677 2007-05-26 1
1677 2007-05-27 1
1677 2007-05-28 1
1677 2007-05-29 1
1677 2007-05-30 1
1677 2007-05-31 1
Table 2 contains
ID Date ChangeCode
1677 2007-05-07 RE
1677 2007-05-14 RA
1677 2007-05-26 DH
Upon completion of the sql statement(s) Table 1 should look like this:
ID Date Status
1677 2007-05-01 1
1677 2007-05-02 1
1677 2007-05-03 1
1677 2007-05-04 1
1677 2007-05-05 1
1677 2007-05-06 1
1677 2007-05-07 RE
1677 2007-05-08 RE
1677 2007-05-09 RE
1677 2007-05-10 RE
1677 2007-05-11 RE
1677 2007-05-12 RE
1677 2007-05-13 RE
1677 2007-05-14 RA
1677 2007-05-15 RA
1677 2007-05-16 RA
1677 2007-05-17 RA
1677 2007-05-18 RA
1677 2007-05-19 RA
1677 2007-05-20 RA
1677 2007-05-21 RA
1677 2007-05-22 RA
1677 2007-05-23 RA
1677 2007-05-24 RA
1677 2007-05-25 RA
1677 2007-05-26 DH
1677 2007-05-27 DH
1677 2007-05-28 DH
1677 2007-05-29 DH
1677 2007-05-30 DH
1677 2007-05-31 DH
The Status listed at a particular date should be the status of the last ChangeCode in Table 2 that was in effect until the date of the next ChangeCode.
Right now I have the following code but it displays the first ChangeCode value that occurs for the rest of the table even when there is a change after it.
UPDATE Table1 SET Status=ChangeCode
FROM Table1 AS t1 INNER JOIN Table2 AS t2
ON (t1.PatientID = t2.PatientID)
WHERE (CONVERT(char(10), t1.Date, 120)>=CONVERT(char(10), t2.Date, 120))
Any assistance would be greatly appreciated.
Sean
Start Free Trial