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

Posted

技术标签:

【中文标题】在 MS ACCESS 中加入两个不同的计数查询【英文标题】:Joining two disctinct count queries in MS ACCESS 【发布时间】:2021-10-13 23:21:03 【问题描述】:

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

我想要完成的是最后有一张表显示 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;

【问题讨论】:

【参考方案1】:

您可以使用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;

【讨论】:

以上是关于在 MS ACCESS 中加入两个不同的计数查询的主要内容,如果未能解决你的问题,请参考以下文章

如何在查询中加入 MS-SQL 和 MySQL 表?

Microsoft Access SQL 查询计数不同

从单独的数据库中加入查询

MS Access 查询在查询中返回 0 个计数

在 Django 过滤器中加入两个模型

ms-access 查询需要连接编号或计数