Access 2003 - 计算唯一值

时间:2011-08-23 12:22:07

标签: sql ms-access

如何计算Access 2003中的唯一值? 当我写这样的时候:

SELECT DISTINCT Customer FROM CustomersTable;

我得到了独特客户的结果,但如果此代码不起作用,请如何计算:

SELECT COUNT(*) 

FROM (SELECT DISTINCT Customer FROM CustomersTable )

(它导致错误:“Microsoft Jet数据库引擎无法找到输入表或查询'SELECT DISTINCT Customer FROM CustomersTable'。确保它存在并且其名称拼写正确”)

数据库示例

--Customer -- Address -- 
X               NY 
X               OR 
Y               AR 
Z               WA 

我希望得到结果3(三个独特的客户。)

4 个答案:

答案 0 :(得分:1)

试试这个:

Select 
    Customer, 
    count(Customer) as count
from
    CustomersTable
group by
    Customer

好的,你现在应该试试这个:

SELECT COUNT(DISTINCT Customer) FROM CustomersTable;

希望这有帮助。

答案 1 :(得分:1)

您需要为子查询命名:

SELECT COUNT(*) 
  FROM (SELECT DISTINCT Customer FROM CustomersTable) AS T

答案 2 :(得分:0)

编辑(查询已更正)

这在Access 2007中对我有用

SELECT Count(*) AS NumCustomers
FROM 
(
    SELECT Distinct CustomerAddress.Customer
    FROM CustomerAddress
)  AS DistinctCustomers;

答案 3 :(得分:0)

我知道这个问题很可能在很久以前就已经放弃了,但只是对于遇到这个问题的人来说,因为我在使用C#应用程序执行类似任务时遇到错误并在此处找到了避难所: http://www.geeksengine.com/article/access-distinct-count.html

这给了我很多帮助,如果破了,给出了以下查询结构

    SELECT COUNT(columnName) as num_ofColumnNameEntries
    from
       (
        SELECT DISTINCT columnName FROM TableName
       )