如何从 patient_id 中找到最频繁出现的数字

How do I find the most frequent number from patient_id

要从中检索数据的 table 的图像:

REM 入学 TABLE

插入入场值 (205,101,'2/2/2011','HB',114,'P','21/2/2011');

按 patient_id 分组计数并使用 FETCH FIRST 仅 1 行:

select count(*) cnt, patient_id 
  from admission group by patient_id 
order by cnt desc FETCH FIRST 1 ROWS ONLY;

如果您的 Oracle < 12C R1,请将 rownum 与子查询一起使用:

select *
from  
(select count(*) cnt, patient_id 
      from admission group by patient_id 
    order by cnt desc ) 
where ROWNUM = 1;

什么是“出现次数最多的号码”?入院次数最多的患者?如果是,那么

select patient_id
from (select patient_id,
             rank() over (order by count(*) desc) rnk
      from admission
      group by patient_id
     )
where rnk = 1;

在这两种情况下,如果您只想 patient_id

,您可以从 select 中删除 count(*) AS patient_max

甲骨文

SELECT * FROM (
 SELECT patient_id,count(*) AS patient_max
           FROM admission 
           GROUP BY patient_id 
           ORDER BY count(*) DESC
 ) 
 WHERE ROWNUM <= 1;

Postgres

 SELECT patient_id,count(*) AS patient_max
           FROM admission 
           GROUP BY patient_id 
           ORDER BY count(*) DESC 
           LIMIT 1;
select patient_id ,count(*) as patient_count
  from admission group by patient_id 
order by patient_count desc FETCH FIRST 1 ROWS ONLY;

如果您只想要 patient_id 那么:

WITH temp as 
(
select patient_id ,count(*) as patient_count
      from admission group by patient_id 
    order by patient_count desc FETCH FIRST 1 ROWS ONLY
)
select patient_id from temp;