Newbie : Updating a table from a query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • taoh15
    New Member
    • Feb 2008
    • 5

    #1

    Newbie : Updating a table from a query

    All

    I have two tables

    Table1 has three fields empid,elid, stats
    Empid and Elid are unique
    Table2 contains many values of empid,elid and apstatid

    I need to do something like this
    UPDATE Table1 SET Table1.stats= (SUM(Table2.aps tatid)/COUNT(Table2.ap statid)) FROM
    Table2 WHERE Table2.empid= Table1.empid AND Table2.elid= Table1.elid

    How do I do it?

    Thanks
  • Delerna
    Recognized Expert Top Contributor
    • Jan 2008
    • 1134

    #2
    You nearly had it!

    Code:
    UPDATE Table1 
    SET Table1.stats = 
    (   SELECT SUM(Table2.apstatid)/COUNT(Table2.apstatid)
        FROM Table2 
        WHERE Table2.empid= Table1.empid 
             AND Table2.elid= Table1.elid 
    )

    Comment

    • taoh15
      New Member
      • Feb 2008
      • 5

      #3
      Thanks

      That did it.

      With a little help from Query Optimizer the 3.5hr operation not takes 3 secs :-)

      Taoh

      Comment

      Working...