使用oracle-11g返回子组的每个最大计数

时间:2014-03-10 09:37:50

标签: sql oracle oracle11g group-by aggregate-functions

我有一张桌子

CREATE TABLE table1
(
   from_id varchar2(10),
   to_id varchar2(10),
   b_id varchar2(20) NOT NULL,
   exp_in_date varchar2(20) NOT NULL
);

示例数据:

Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','2','20140203056',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','2','20140203056',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','1','20140203056',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','2','20140203057',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('2','5','20140203057',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','1','20140203057',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','2','20140203058',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('2','5','20140203058',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','1','20140203058',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','2','20140203059',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('2','5','20140203059',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','1','20140203059',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','2','20140204059',TO_DATE('20140204', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','2','20140204059',TO_DATE('20140204', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','2','20140204059',TO_DATE('20140204', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','2','20140204059',TO_DATE('20140204', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','2','20140204059',TO_DATE('20140204', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','1','20140203060',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','1','20140203060',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','1','20140203060',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','1','20140203060',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','4','20140203061',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','4','20140203061',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','4','20140203061',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','4','20140203061',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','4','20140203061',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','4','20140203061',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','4','20140203061',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','3','20140203062',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','3','20140203062',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','3','20140203062',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','3','20140203062',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','3','20140203062',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','1','20140203063',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','1','20140203063',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','1','20140203063',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','1','20140203063',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','1','20140203064',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','1','20140203064',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','1','20140203064',TO_DATE('20140203', 'yyyymmdd'));
Insert into TABLE1 (FROM_ID,TO_ID,B_ID,EXP_IN_DATE) values ('5','1','20140203064',TO_DATE('20140203', 'yyyymmdd'));

我想获得每个'4' b_id的最大计数(*),每个4 b_id代表一小时内的交易,假设

  

20140203056至20140203059

e.g。预期结果

Date-hr                     from_id to_id   count(no_record)
------------------------------------------------------------
February, 03 2014 14:00     2       5       1   
February, 03 2014 14:00     5       1       1   

我可以做简单的分组。我想我可以在PLSQL中编写代码,但该查询的性能非常重要。如果没有更好的解决方案,我想执行for循环以及if(更大)而不是比较。

1 个答案:

答案 0 :(得分:0)

你的意思是这样吗?

select count(*) countx,from_id,to_id,exp_in_date
from table1
group by from_id,to_id,exp_in_date;

请参阅fiddle