views:

63

answers:

2

I have 3 values that I need to copy from one table to another table. Here is my amateur attempt at it, I know it is horribly ineffecent, what would the correct way of doing this query?

  update [IDAT_PATIENTS] 
  set TargetRabiesSerial = 
    (select top 1 SERIAL_NUMBER 
     from [IDAT_RABIESHISTORY] as rab 
     where TargetPetAccountNum = rab.PATIENT_ACCOUNT_ID 
        and TargetClientAccountNum = rab.CLIENT_ACCOUNT_ID 
     order by rab.DATE_TIME_PERFORMED desc)
  , TargetRabiesBrandName = 
    (select top 1 BRAND_NAME 
     from [IDAT_RABIESHISTORY] as rab 
     where TargetPetAccountNum = rab.PATIENT_ACCOUNT_ID 
        and TargetClientAccountNum = rab.CLIENT_ACCOUNT_ID 
     order by rab.DATE_TIME_PERFORMED desc)
  , TargetRabiesTag = 
    (select top 1 TAG_NUMBER 
     from [IDAT_RABIESHISTORY] as rab 
     where TargetPetAccountNum = rab.PATIENT_ACCOUNT_ID 
        and TargetClientAccountNum = rab.CLIENT_ACCOUNT_ID 
     order by rab.DATE_TIME_PERFORMED desc)
  where TargetClientAccountNum in 
    (select CLIENT_ACCOUNT_ID 
     from [IDAT_RABIESHISTORY] 
     where TargetPetAccountNum = PATIENT_ACCOUNT_ID)
+3  A: 

Try This:

  Update p Set
     TargetRabiesSerial = h.SERIAL_NUMBER,
     TargetRabiesBrandName =  h.BRAND_NAME,  
     TargetRabiesTag = h.TAG_NUMBER   
  From IDAT_PATIENTS p
      Join IDAT_RABIESHISTORY h
          On h.PATIENT_ACCOUNT_ID = p.TargetPetAccountNum
             And h.CLIENT_ACCOUNT_ID = p.TargetClientAccountNum 
             And h.DATE_TIME_PERFORMED = 
                    (Select Max(DATE_TIME_PERFORMED)
                     From IDAT_RABIESHISTORY 
                     Where h.CLIENT_ACCOUNT_ID = p.TargetClientAccountNum 
                         And h.CLIENT_ACCOUNT_ID = p.TargetClientAccountNum)
  Where TargetClientAccountNum In  
    (Select CLIENT_ACCOUNT_ID  
     From IDAT_RABIESHISTORY  
     Where TargetPetAccountNum = PATIENT_ACCOUNT_ID) 
Charles Bretana
When I run your code I get 1 row affected. I know I have more than one record that needs updating. (my old code has 136107 rows affected)
Scott Chamberlain
I think the issue is you are pulling form the most recent record overall, i need the most recent record from each patient.
Scott Chamberlain
So, for each patient, you want to update ALL the records, not just the one that has the latest DATE_TIME_PERFORMED ? but you want to update the columns from that patient's record which has the latest DATE_TIME_PERFORMED? But this doesn't make sense to me, the fields you're updating are in the patient table, not in the history table... I am not understanding you...
Charles Bretana
My join is joining, for each patient, the most recent History record for that specific patient. (look at Join conditions)...
Charles Bretana
Also, explain what the difference is between PATIENT_ACCOUNT_ID and CLIENT_ACCOUNT_ID... Which of these identifies the group you want the most recent record from (to do the update) One of these?, or the other? or both?
Charles Bretana
A: 

Something more or less like this, didn't have a schema to validate against so double check the syntax :)

update [IDAT_PATIENTS] 
    set TargetRabiesSerial = tblLatestHistory.SERIAL_NUMBER,
    TargetRabiesBrandName = IDAT_RABIESHISTORY,
    TargetRabiesTag = IDAT_RABIESHISTORY
from [IDAT_PATIENTS]
inner join (
    select PATIENT_ACCOUNT_ID, CLIENT_ACCOUNT_ID, SERIAL_NUMBER, BRAND_NAME, TAG_NUMBER, max(DATE_TIME_PERFORMED) 
    from IDAT_RABIESHISTORY
    group by  PATIENT_ACCOUNT_ID, CLIENT_ACCOUNT_ID, SERIAL_NUMBER, BRAND_NAME, TAG_NUMBER
) tblLatestHistory
on TargetPetAccountNum = tblLatestHistory.PATIENT_ACCOUNT_ID 
and TargetClientAccountNum = tblLatestHistory.CLIENT_ACCOUNT_ID 
jfrobishow