如何计算SQL中唯一出现的数据

时间:2011-06-17 18:04:11

标签: mysql sql ms-access

我试图在以下代码中计算客户ID的唯一出现次数。

TRANSFORM Count(Research.Client_ID) AS CountOfClient_ID
SELECT Research.Treatment, Count(DCount("[Client_ID]","[Letter Status]")) AS [Total Letters Sent]
FROM Research INNER JOIN [Letter Status] ON Research.Client_ID = [Letter Status].Client_ID
GROUP BY Research.Treatment
PIVOT [Letter Status].Letter_Status;

我认为需要修改的表达是:

Total Letters Sent: Count(DCount("[Client_ID]","[Letter Status]"))

DCount函数的典型形式是(表达式,域, * 条件 *)。我很确定我需要以某种方式指定[Client_ID]在DCount函数的标准参数中应该是唯一的,但我不知道如何。这可能吗?

1 个答案:

答案 0 :(得分:2)

如果此查询无法获得您想要的内容,请向我们展示样本数据与您想要的不同之处。

SELECT
    q.Treatment,
    Count(*) AS [Total Letters Sent]
FROM
    [SELECT DISTINCT
        Treatment,
        Client_ID
    FROM
        Research
    ]. AS q
GROUP BY
    q.Treatment;