查找任何患者的最大儿童数量

时间:2017-10-24 15:25:25

标签: oracle

我在oracle中有一个看起来像这样的表:

%call importdata() after changing current directory
B = importdata("file.txt");

d  = 1000; %vertical spacing
Bs = B;    %copy of the original data

for i = 1 : size(Bs,2)
    %loop adding constant to each column
    Bs(:,i) = Bs(:,i) + (i-1) * d;
end    

%plot the modified matrix
plot(Bs);

如何从此列表中找到最大PID数?

3 个答案:

答案 0 :(得分:1)

您可以使用ORDER BYrownum。这比使用窗口函数简单得多,因为您只需要最高的计数。

 select pid , ct FROM
(
select pid, count(C_ID)  ct
from TABLE1 group by PID 
  ORDER by ct DESC
 ) where rownum = 1

答案 1 :(得分:0)

您可以按PID分组并计算行数。此数字必须等于从子查询中选择的最大计数

 SELECT PID, COUNT(*) As NoOfChildren
 FROM MyTable
 GROUP BY PID
 HAVING COUNT(*) =
     (SELECT MAX(NoOfChildren) FROM
         (SELECT COUNT(*) AS NoOfChildren
          FROM MyTable
          GROUP BY PID)
     )

结果是

PID     NOOFCHILDREN
P520    4

请参阅:http://sqlfiddle.com/#!4/64de5/1/0

答案 2 :(得分:0)

您可以使用窗口函数row_number:

select * from (
    select pid, 
        count(*) as cnt, 
        row_number() over (order by count(*) desc) as rn
    from your_table t
    group by pid
) t where rn = 1;

如果您想要所有pid(如果多次存在多次),则可以将row_number替换为rank