Advertisement

07.25.2007 at 06:13PM PDT, ID: 22721646
[x]
Attachment Details

Update SQL table based on second table using date ranges

Asked by citnetworks in Databases Miscellaneous, MS SQL Server, SQL Query Syntax

Tags: could, dates, sql, table, update

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
[+][-]07.25.2007 at 07:32PM PDT, ID: 19571465

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.25.2007 at 08:22PM PDT, ID: 19571649

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.25.2007 at 08:33PM PDT, ID: 19571683

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.25.2007 at 08:33PM PDT, ID: 19571686

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.25.2007 at 08:55PM PDT, ID: 19571749

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.25.2007 at 08:57PM PDT, ID: 19571756

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.25.2007 at 09:03PM PDT, ID: 19571773

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.25.2007 at 09:18PM PDT, ID: 19571820

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.25.2007 at 09:35PM PDT, ID: 19571850

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Databases Miscellaneous, MS SQL Server, SQL Query Syntax
Tags: could, dates, sql, table, update
Sign Up Now!
Solution Provided By: appari
Participating Experts: 2
Solution Grade: A
 
 
[+][-]07.25.2007 at 10:45PM PDT, ID: 19572122

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32