If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Urgent SQL question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-28-06, 06:05
kellen kellen is offline
Registered User
 
Join Date: Aug 2006
Posts: 2
Urgent SQL question

Hi,

I have 2 tables record patient id, inpatient and outpatient date.
Out_Patient table:

p_id, out_date
001, 2001/1/1
001, 2002/2/15
001, 2002/3/7
002, 2002/6/6
002, 2004/5/20
003, 2003/7/8

In_Patient table:

p_id, In_date, Last_Out_date
001, 2000/12/21
001, 2002/2/10
001, 2002/3/5
002, 2002/6/1
002, 2004/5/18
003, 2003/7/7

When a patient comes in, I need to retrieve his previous out_date and update it in the Last_Out_date Column. So the result of In_Patient table suppose to be:

p_id, In_date, Last_Out_date
001, 2000/12/21
001, 2002/2/10, 2001/1/1
001, 2002/3/5, 2002/2/15
002, 2002/6/1
002, 2004/5/18, 2002/6/6
003, 2003/7/7

I've used min() function but didn't work the way I wanted. I'd appreciate if anyone could tell me how to do this.
Reply With Quote
  #2 (permalink)  
Old 08-28-06, 07:45
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
update In_Patient 
   set Last_Out_date = t2.out_date
  from In_Patient t1
inner
  join Out_Patient t2
    on t1.p_id = t2.p_id
 where t2.out_date 
     = ( select max(out_date)
           from Out_Patient
          where t2.p_id )
please note that the UPDATE... FROM... syntax is for microsoft sql server
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-28-06, 08:20
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Quote:
Originally Posted by r937
please note that the UPDATE... FROM... syntax is for microsoft sql server
While not all SQL dialects implement this handy non-standard construct, it isn't just a Microsoft feature...

-PatP
Reply With Quote
  #4 (permalink)  
Old 08-30-06, 15:24
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Or without this "nonstandard" feature:
(at least, if I understand the problem correctly)
Code:
UPDATE In_Patient AS t1
   SET Last_Out_date = (SELECT max(out_date)
                        FROM   Out_Patient
                        WHERE  p_id = t1.p_id
                          AND  out_date < t1.in_date)
Note that nothing is set when the subquery finds no out_dates older than in_date, which is exactly what is needed.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #5 (permalink)  
Old 08-31-06, 22:14
kellen kellen is offline
Registered User
 
Join Date: Aug 2006
Posts: 2
Thanks Peter. That was what i was actually looking for.

Regards Kellen
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On