Oracle组/计数查询

时间:2011-12-13 16:11:13

标签: oracle count group-by

我有以下两个表:

TableOne
========
Id1|ColA1|ColB1|ColC1|ColD1|ColE1
--------------------------------
  1| AFoo|BFoo |CFoo | DFoo| EFoo
  2| AFoo|BBar |CFoo | DFoo| EFoo

TableTwo
========
Id2|ColA2|ColB2|ColC2
---------------------
 11| 1   |ABC  |NOP  |
 12| 1   |ABC  |QRS  |
 13| 1   |DEF  |TUV  |
 14| 1   |DEF  |WXY  | 
 15| 1   |DEF  |FGH  | 
 16| 2   |ABC  |NOP  | 

我运行以下查询:

select t1.*, t2.*
from TableOne t1
inner join TableTwo t2 on t2.ColA2=t1.Id1
where t1.ColA1='AFoo'

并获得以下结果:

Result
======
Id1|ColA1|ColB1|ColC1|ColD1|ColE1|Id2|ColA2|ColB2|ColC2
-------------------------------------------------------
  1| AFoo|BFoo |CFoo | DFoo| EFoo| 11| 1   | ABC | NOP
  1| AFoo|BFoo |CFoo | DFoo| EFoo| 12| 1   | ABC | QRS
  1| AFoo|BFoo |CFoo | DFoo| EFoo| 13| 1   | DEF | TUV
  1| AFoo|BFoo |CFoo | DFoo| EFoo| 14| 1   | DEF | WXY
  1| AFoo|BFoo |CFoo | DFoo| EFoo| 15| 1   | DEF | FGH
  2| AFoo|BBar |CFoo | DFoo| EFoo| 16| 2   | ABC | NOP

我真正想要的是:

Desired Result
======
Id1|MaxDup
----------------------------------------
  1| 3 (This is because there are 3 DEF records)
  2| 1 (This is because there is 1 ABC record)

因此,我试图跟踪每个TableOne行显示的ColB2中出现的最大次数。在上面的示例中,ID1为1有两条ABC记录和三条与之关联的DEF记录。由于DEF记录多于ABC记录,我想要返回的DEF记录数。

任何人都可以提供一个可以证明这一点的工作示例吗?

4 个答案:

答案 0 :(得分:1)

试试这个(我还没有测试过):

with t2 as (
    select  ColA2, ColB2, count(*) cnt 
    from TableTwo
    group by ColA2, ColB2 
)
select t1.Id1,
(  select max(cnt) MaxDup
   from t2
   where t2.ColA2=t1.Id1)
from TableOne t1

答案 1 :(得分:0)

它的SQL Server代码,但我认为它可以安全地转移到Oracle:

    select  id1,cola1,colb1,colc1,cold1,cole1,max(dup) as dup2  from (
        select t1.id1, t1.cola1, t1.colb1, t1.colc1,t1.cold1,t1.cole1, t2.colb2,count(*) as dup
        from t1 
        inner join  t2  on t2.cola2=t1.Id1
        where t1.cola1='Afoo'
        group by t1.id1, t1.cola1, t1.colb1, t1.colc1,t1.cold1,t1.cole1, t2.colb2) 
    tab
    group by id1,cola1,colb1,colc1,cold1,cole1

它适用于Sql Server,通过包含在子查询中,您只能提取id1和dup2。尝试扩展您的数据集,它可以工作。

答案 2 :(得分:0)

以下是基于测试数据的结果。

SQL> select       
        cola2
        , cnt as maxDup
from (        
        select       
                cola2
                , colb2
                , cnt
                , rank() over (partition by cola2 order by cnt desc ) cnt_rnk
        from (
                select cola2
                       , colb2
                       , count(*) as cnt
                from  TableTwo
                group by cola2, colb2
        )
    )        
where     cnt_rnk = 1
/
  2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19  
COLA     MAXDUP
---- ----------
1             3
2             1

SQL>

最里面的查询计算副本。中间查询对重复的计数进行排名。最外层查询过滤具有最高计数的组合:

答案 3 :(得分:0)

也许这个?

SELECT t2.id2 id, count(t2.ColB2) MaxDup
FROM TableTwo t2
WHERE EXISTS(
      SELECT * FROM TableOne t1
      WHERE t1.id1 = t2.id2
      AND t1.ColA1='AFoo')
AND rownum < 2
GROUP BY t2.id2, t2.ColB2
ORDER BY MaxDup desc;