Results 1 to 9 of 9
  1. #1
    TrevorPhillips is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    4

    SQL to UPDATE column from table using the COUNT result of a joined table


    I'm trying to update a column from table1 using the count result of joined table1 and table2.
    Code:
    UPDATE table1
    SET column1 = (SELECT COUNT(*) FROM table1
    INNER JOIN table2 ON table1.column = table2.column);

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Why are you saving aggregate data? Saving calculated data is usually a bad idea, especially aggregate data. Calculate when needed.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    TrevorPhillips is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    4
    I'm using the MS access SQL and I want to COUNT an INNER joined table and then UPDATE a field based on the result of the COUNT. How can I do that using a query or queries.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Not sure can. Seems I have tried updating a table with an aggregate query output and get 'not updatable' error because the aggregate query is involved.

    As pointed out, should not save aggregate data. Why do you feel it is necessary?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Did you really want to update every individual record in table1 to have the identical count of how many matches might be made between that entire table1 and another table2? Or are you trying to update each record in table1 with the count of how many records in table2 match that particular record?

    The first can be done in two steps using VBA. I just can't imagine the application that needs that.

    The second can be done with a subquery, but it won't look exactly like you're showing it.

  6. #6
    TrevorPhillips is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    4
    Quote Originally Posted by Dal Jeanis View Post
    Did you really want to update every individual record in table1 to have the identical count of how many matches might be made between that entire table1 and another table2? Or are you trying to update each record in table1 with the count of how many records in table2 match that particular record?

    The first can be done in two steps using VBA. I just can't imagine the application that needs that.

    The second can be done with a subquery, but it won't look exactly like you're showing it.
    I'm trying to update every record on table1 with the count of records of table1 and table2 but forgot to put WHERE column1 = table2's foreign key?

  7. #7
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    So maybe you wanted something more like this?
    Code:
    UPDATE table1
    SET column1 = 
      (SELECT COUNT(*) 
       FROM table2 
       WHERE table1.column = table2.column);

  8. #8
    TrevorPhillips is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    4
    Quote Originally Posted by Dal Jeanis View Post
    So maybe you wanted something more like this?
    Code:
    UPDATE table1
    SET column1 = 
      (SELECT COUNT(*) 
       FROM table2 
       WHERE table1.column = table2.column);
    Table1 name = flight
    Table1 primarykey = flightnumber
    Table1 column = seattaken

    Table2 name = tickets
    Table2 column = flightnumber

    What I intend to do is to update all records of [seattaken] of [flight] where [flightnumber] is equal to [tickets] [flightnumber]

    It seems the problem is the MS access 2007 update query not working(the database is saved as 2002-2003 format so that it will work it vb6). I tried updating a record but it doesn't change the value. Is there a way to fix it?

  9. #9
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    You need to create a temp table to hold your intermediate results.
    Three steps:
    Code:
    SELECT TT.flightnumber, Count(1) AS ticketCount INTO tempTicketCount
    FROM tickets AS TT
    GROUP BY TT.flightnumber;
    
    UPDATE [flights] AS TF, [tempTicketCount] AS TT
    SET TF.[seattaken] = TT.ticketCount
    WHERE TF.[flightnumber] = TT.[flightnumber];
    
    DELETE FROM tempticketcount;

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 09-04-2013, 06:40 AM
  2. Replies: 11
    Last Post: 08-10-2012, 10:25 PM
  3. Replies: 8
    Last Post: 03-18-2012, 11:17 AM
  4. Replies: 1
    Last Post: 12-16-2011, 08:16 AM
  5. Multiple joins, avoiding cross joined result
    By richjhart in forum Queries
    Replies: 2
    Last Post: 09-17-2010, 09:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums