MS 访问计数不同的多列
Posted
技术标签:
【中文标题】MS 访问计数不同的多列【英文标题】:MS Access Count Distinct Multiple Columns 【发布时间】:2018-02-23 20:23:50 【问题描述】:我一直在尝试使用 Count distinct 编写基于 SQL 的查询,但遇到了如何正确构建 Microsoft Access 格式的问题。
我当前的查询格式如下,如果可能的话,我想将 Count 操作转换为 distinct count:
SELECT
a.Customer_ID, a.First_Name,
COUNT (b.Payment_Method) AS Payment_Method <- would like to get the distinct count
Count (b.Store_Number) as Store_Number <- would like to get the distinct count
SUM (b.Sales_Amount) AS Sales_Amount
FROM Table1 a
INNER JOIN Table2 b ON a.Customer_ID = b.Customer_ID
WHERE (((a.Date_ID) > 1234))
GROUP BY a.Customer_ID, a.First_Name
我熟悉此查询的 SQL 构造,并希望使用 MS-Access 重新创建结果。
【问题讨论】:
必须首先构建一个返回 Distinct 记录的查询。然后在另一个查询中使用该查询来计算这些记录。 ***.com/questions/11880199/… 【参考方案1】:这比其他数据库复杂得多,但你可以在加入之前:
SELECT a.Customer_ID, a.First_Name, p.num_pm, s.num_sn, s.sum_sa
FROM (Table1 as a INNER JOIN
(SELECT CustomerId, COUNT(*) as num_pm
FROM (SELECT DISTINCT CustomerId, Payment_Method FROM Table2) as b
GROUP BY CustomerId
) as p
ON a.CustomerId = p.CustomerId
) INNER JOIN
(SELECT CustomerId, COUNT(*) as num_sn, SUM(sa) as sum_sa
FROM (SELECT CustomerId, Store_Number, SUM(Sales_Amount) as sum_sa
FROM Table2
GROUP BY CustomerId, Store_Number
) as b
GROUP BY CustomerId
) as s
ON s.CustomerId = a.CustomerId
WHERE a.Date_ID > 1234
【讨论】:
以上是关于MS 访问计数不同的多列的主要内容,如果未能解决你的问题,请参考以下文章