在 MS ACCESS 中加入两个不同的计数查询

Joining two disctinct count queries in MS ACCESS

我正在努力在 MS Access 中合并以下两个查询。第一个查询基本上是计算每周有多少商店的产品(pcode)可用。第二个查询统计每周的门店数量。

我想要完成的是最后有一个table显示WEEK_ID,PCODE,store_number_per_pcode_per_week,store_number_per_week。你能给我一些提示吗?

1。查询

SELECT cd.WEEK_ID, cd.PCODE, count(cd.STORE_ID) as store_number_per_pcode_per_week
FROM (SELECT DISTINCT STORE_ID, PCODE, WEEK_ID
      FROM SALES
     ) as cd
GROUP BY cd.WEEK_ID, cd.PCODE;

2。查询

SELECT df.WEEK_ID, count(df.STORE_ID) as store_number_per_week
FROM (SELECT DISTINCT STORE_ID, WEEK_ID
      FROM SALES
     ) as df
GROUP BY df.WEEK_ID;

您可以使用 JOIN:

SELECT wp.WEEK_ID, wp.PCODE, wp.store_number_per_pcode_per_week,
       w.store_number_per_week
FROM (SELECT cd.WEEK_ID, cd.PCODE,
             COUNT(cd.STORE_ID) as store_number_per_pcode_per_week,
      FROM (SELECT DISTINCT STORE_ID, PCODE, WEEK_ID
            FROM SALES
           ) as cd
      GROUP BY cd.WEEK_ID, cd.PCODE
     ) wp INNER JOIN
     (SELECT cd.WEEK_ID, 
             COUNT(cd.STORE_ID) as store_number_per_week,
      FROM (SELECT DISTINCT STORE_ID, WEEK_ID
            FROM SALES
           ) as cd
      GROUP BY cd.WEEK_ID
     ) w
     ON wp.WEEK_ID = w.WEEK_ID;