【问题标题】:SELECT(DISINCT) in MS Access [duplicate]MS Access 中的 SELECT(DISTINCT) [重复]
【发布时间】:2020-07-01 11:49:33
【问题描述】:

我在 MS Access 中的其他 SELECT(DISINCT) 类型的问题中找不到问题的解决方案,我已经为此苦苦挣扎了好几个小时;

我有一个 Photos 表,其中包含两列:PhotoTypeIdPatientId。每个患者都可以多次拍摄多种类型的照片。

例子:

PhotoTypeId,PatientId  
13,1050  
14,1050  
13,1050  
13,1051  
13,1054

说明:
患者#1050 拍摄了1314 类型的照片,而患者#1051#1054 仅拍摄了13 类型的照片。

我要查找的是为最独特的患者拍摄的照片类型。在上面的例子中,它是照片类型13,因为它是针对3个不同的患者制作的(#1050两次,#1051#1054

我必须在 MS Access 中执行此操作,并且我想使用 SQL 执行此操作。我试过类似的东西:

SELECT PhotoId, COUNT(DISTINCT PatientId)
FROM Photos
GROUP BY PhotoId;

但 MS Access 不支持 COUNT(DISTINCT x) 语法;怎么办?

【问题讨论】:

    标签: sql ms-access count ms-office distinct


    【解决方案1】:

    使用子查询,因为 MS 访问不支持该语法:

    select top 1 p.PhotoId, count(*)
    from (select distinct PhotoId, PatientId from Photos
         ) as p
    group by p.PhotoId
    order by count(*) desc;
    

    【讨论】:

      【解决方案2】:

      MS Access 不支持count(disitnct)。但你可以这样做:

      select top 1 photoid, count(*)
      from (select distinct photoid, patientid
            from photos
           ) as pp
      group by photoid
      order by count(*) desc;
      

      MS Access 将top 1 视为top 1 with ties。如果您只想要一行,则更改order by

      select top 1 photoid, count(*)
      from (select distinct photoid, patientid
            from photos
           ) as pp
      group by photoid
      order by count(*) desc, photoid;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-21
        • 2015-12-28
        相关资源
        最近更新 更多