简体   繁体   中英

Update a MS Access field with the column count of another, not JOINed table

What I'm trying to do is create an update query in MS Access 2013 for a table separate from the actual data tables ( meaning that there is no connection between the data table and the statistics table ) to store some statistics (eg Count of records) that need to be stored for further calculations and later use.

I've looked up a bunch of tutorials in the past few days on this, with no luck of finding a solution to my problem, as all solutions included joining the tables, which - in my case - is irrelevant, as the table to-be-calculated-on is temporary with constantly changing data, thus I always want to count every record, find the max in the whole temp table, etc. on a given date (like logging).

The structure of statisticsTable :

| statDate (Date/time) | itemCount (integer) | ... |
----------------------------------------------------
|           01/01/2017 |                  50 | ... |
|           02/01/2017 |                  47 | ... |
|           03/01/2017 |                  43 | ... |
|                  ... |                 ... | ... |           

What I want to do, in semi-gibberish code:

UPDATE statisticsTable
SET itemCount = (SELECT Count(*) FROM tempTable)
WHERE statDate = 01/01/2017;

This should update the itemCount field of 01/01/2017 in the statisticsTable with the current row count of the temp table.

I know that this might not be the standard OR the correct use of MS Access or any DBMS in general, however, my assignment is rather limited, meaning I can't (shouldn't) modify any table structures, connections or the database structure in general, only create the update query that works as described above.

Is it possible to update a table's field value with the output of a query calculating on another table, WITHOUT joining the two tables in MS Access?

EDIT 1:

After further research, the function DCount() might be able to give the results I'm looking for, I will test it.

EDIT : I wrote a way more complicated answer that might not have even worked in Access (it would work in MS SQL-Server). Anyway.

What you need is a join criteria that is always true on which to base your update. You can just use is not null :

SELECT s.*, a.itemCount 
FROM statisticsTable as s
INNER JOIN 
    (
    SELECT count(*) as itemCount
    from tempTable
    ) as a
on s.[some field that is always populated] is not null
    and a.itemCount is not null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM