SELECT(DISTINCT) 在 MS Access 中

SELECT(DISINCT) in MS Access

我在其他 SELECT(DISINCT) in MS Access 类型的问题中找不到问题的解决方案,我一直在努力解决好几个小时;

我有一个 table Photos 有两列:PhotoTypeIdPatientId。每个患者都可以多次制作多种类型的照片。

示例:

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

描述:
患者 #1050 拍了 1314 类型的照片,而患者 #1051#1054 只拍了 13.[=23 类型的照片=]

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

我必须在 MS Access 中完成,我想使用 SQL 完成。我试过类似的东西:

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

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

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;

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

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