简体   繁体   English

计算不同的查询MS Access

[英]Count distinct query MS Access

It seems that we can not use Count (Distinct column) function in MS Access. 看来我们无法在MS Access中使用Count (Distinct column)功能。 I have following data and expected result as shown below 我有以下数据和预期结果,如下所示

Looking for MS Access query which can give required result. 寻找可以提供所需结果的MS Access查询。

Data 数据

ID     Name     Category    Person  Office
1      FIL       Global     Ben     london
1      FIL       Global     Ben     london
1      FIL       Overall    Ben     Americas
106   Asset      Global     Ben     london
156   ICICI      Overall    Rimmer  london
156   ICICI      Overall    Rimmer  london
188   UBS       Overall     Rimmer  london
9    Fund      Global       Rimmer  london

Expected Result 预期结果

Person  Global_Cnt  Overall_Cnt    
Ben          2         1
Rimmer       1         2

Use a subquery to select the distinct values from your table. 使用子查询从表中选择不同的值。

In the parent query, GROUP BY Person , and use separate Count() expressions for each category. 在父查询GROUP BY Person ,并对每个类别使用单独的Count()表达式。 Count() only counts non-Null values, so use IIf() to return 1 for the category of interest and Null otherwise. Count()仅计算非Null值,因此请使用IIf()为感兴趣的类别返回1,否则返回Null。

SELECT
    sub.Person,
    Count(IIf(Category = 'Global', 1, Null)) AS Global_Cnt,
    Count(IIf(Category = 'Overall', 1, Null)) AS Overall_Cnt
FROM
    (
        SELECT DISTINCT ID, Category, Person
        FROM YourTable
    ) AS sub
GROUP BY sub.Person;

I was unsure which fields identify your unique values, so chose ID , Category , and Person . 我不确定哪个字段标识您的唯一值,因此选择IDCategoryPerson The result set from the query matches what you asked for; 查询的结果集与您要求的相匹配; change the SELECT DISTINCT field list if it doesn't fit with your actual data. 如果它与您的实际数据不匹配,请更改“ SELECT DISTINCT字段列表。

When creating a query in Microsoft Access, you might want to return only distinct or unique values. 在Microsoft Access中创建查询时,您可能只想返回不同或唯一的值。 There are two options in the query's property sheet, "Unique Values" and "Unique Records": 查询的属性表中有两个选项,“唯一值”和“唯一记录”:

DISTINCT and DISTINCTROW sometimes provide the same results, but there are significant differences: DISTINCT和DISTINCTROW有时会提供相同的结果,但是存在显着差异:

DISTINCT DISTINCT checks only the fields listed in the SQL string and then eliminates the duplicate rows. DISTINCT DISTINCT仅检查SQL字符串中列出的字段,然后消除重复的行。 Results of DISTINCT queries are not updateable. DISTINCT查询的结果不可更新。 They are a snapshot of the data. 它们是数据的快照。

DISTINCT queries are similar to Summary or Totals queries (queries using a GROUP BY clause). DISTINCT查询类似于“摘要”或“总计”查询(使用GROUP BY子句的查询)。

DISTINCTROW DISTINCTROW, on the other hand, checks all fields in the table that is being queried, and eliminates duplicates based on the entire record (not just the selected fields). 另一方面, DISTINCTROW DISTINCTROW会检查表中要查询的所有字段,并根据整个记录(而不仅仅是选定的字段)消除重复项。 Results of DISTINCTROW queries are updateable. DISTINCTROW查询的结果是可更新的。

Read More... 阅读更多...

select count(column) as guessTable
from
(
    select distinct column from Table
)

MS Access-Engine does not support MS Access-Engine不支持

 SELECT count(DISTINCT....) FROM ...

You have to do it like this: 您必须这样做:

 SELECT count(*) 
 FROM
 (SELECT DISTINCT Name FROM table1)

Its a little workaround... you're counting a DISTINCT selection. 有点解决方法...您正在计算一个DISTINCT选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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