问题
I have a question. Is it possible to use a table as a query criteria to count how many items in another table?
I have two tables. Table 1 is the query criteria. Table 2 is a table with many data.
I'd like to have a new table or insert into an existing table like the figure below.
Count number of items from table 2 with criteria from table 1. I can run the query many times to count the data of different years. So the main problem is how to count the items use another table as criteria.
I have no idea how to achieve this. Hope you may help me with this.
Thanks a lot for your help >"
回答1:
The following query is the closest I can get for you. rows for C5 and C6 would be missing as no value exists for them.
TRANSFORM Nz(Count([number]),0) AS CountValue
SELECT Table1.ID
FROM Table1, Table2
WHERE (((Table2.number) Between [table1].[start] And [table1].[end]))
GROUP BY Table1.ID
PIVOT DatePart("yyyy",[ndate]);
yeilds:
ID 2000 2001
C1 2 0
C2 2 0
C3 1 0
C4 0 2
C7 1 0
most likely with a larger dataset this answer may yeild some incorrect values to the lack of a definite join, but hopefully it points you in the correct direction to try, once you incorporate an appropriate join in your your tables.
来源:https://stackoverflow.com/questions/31743328/ms-access-use-table-field-as-query-criteria