SQL选择最大值(计数(列A,列B))

时间:2012-11-12 17:23:53

标签: sql oracle

我一直在敲打这个。我想我很亲密。 (Oracle,SQL)

我有一张如下所示的表格。

Company    Code
Apple      A
Google     A
Microsoft  B
Apple      C
Google     B
Microsoft  B
Apple      C
Google     C
Microsoft  B

每家公司都可以解析多个代码。 我想要做的是创建一个SQL语句,每个公司都会给公司提供最常出现的代码。所以在我的例子中我会得到

Apple      C
Google     <nothing since there's no clear max>
Microsoft  B

我到目前为止所做的是以下内容。这个查询返回给我公司的代码看起来最多,但如果我在一个公司的两个代码之间有一个关系,我会回来。对于我的例子中的Google,我会得到(谷歌,A),(谷歌,B),(谷歌,C)。我想要什么都不回来。

我相信我可以再次加入整个事情和一些额外的where子句来过滤掉重复的公司,但我想知道是否有更好的方法来做到这一点。什么杀了我的是聚合函数以及组,因为有时我得到Oracle单组组错误。任何建议都表示赞赏。

SELECT m1, c.company sp1, b.code ul1 FROM
  (SELECT MAX(c1) m1, company FROM
    (SELECT COUNT(company||code) c1, company, code FROM table GROUP BY company, code ) a
  GROUP BY company) c
left OUTER JOIN
  (SELECT COUNT(company||code) c1, company, code FROM table GROUP BY company, code ) b
ON c.company=b.company and
m1=b.c1;

MJ

3 个答案:

答案 0 :(得分:3)

你可以这样做:

select company, case when c > 1 then null else code1 end code1
  from (select company, code1, recs, count(*) over (partition by company, recs ) c, 
           row_number() over (partition by company order by recs desc) rn
  from (select company, code1, count(*) recs
          from table
         group by company, code1))
 where rn = 1
 order by 1

打破这个:

select company, code1, count(*) recs
 from table
 group by company, code1

这使我们每个公司都有他们的代码数量:

COMPANY   C       RECS
--------- - ----------
Google    A          1
Google    B          1
Microsoft B          3
Apple     A          1
Apple     C          2
Google    C          1
从这个我们想要最受欢迎。我们通过分析来做到这一点:

select company, code1, recs,
       row_number() over (partition by company order by recs desc) rn
  from (select company, code1, count(*) recs
          from t1
         group by company, code1)
给予:

COMPANY   C       RECS         RN
--------- - ---------- ----------
Apple     C          2          1 <- we want all rn= "1" rows
Apple     A          1          2
Google    A          1          1<- we want all rn= "1" rows
Google    B          1          2
Google    C          1          3
Microsoft B          3          1<- we want all rn= "1" rows

但现在如果有重复(如谷歌那样)..我们计算(*)RN = 1的行。

select company, code1, recs,
       row_number() over (partition by company order by recs desc) rn,
       count(*) over (partition by company, recs ) c
  from (select company, code1, count(*) recs
          from t1
         group by company, code1)

COMPANY   C       RECS         RN          C
--------- - ---------- ---------- ----------
Apple     C          2          1          1
Apple     A          1          2          1
Google    A          1          1          3
Google    B          1          2          3
Google    C          1          3          3
Microsoft B          3          1          1

所以我们需要说RN = 1和c = 1(即只有一行有recs数。所以我们最终得到:

select company, case when c > 1 then null else Code1 end Code1
  from (select company, code1, recs, count(*) over (partition by company, recs ) c, 
           row_number() over (partition by company order by recs desc) rn
  from (select company, code1, count(*) recs
          from t1
         group by company, code1))
 where rn = 1

即rn = 1且c> 1。在顶部的情况下进行1次检查(因为我们不想过滤掉行,只需将它们标记为不明确。

答案 1 :(得分:1)

尝试

with
tcount as
(
select t.company, t.code, count(*) c
from table1 t 
group by t.company, t.code) 

select distinct tt.company,
decode(count(tt.company) over(partition by tt.company),
1,
tt.code,
null)
from tcount tt
where tt.c =
(select max(c) from tcount tti where tt.company = tti.company) 

Here是一个小提琴

答案 2 :(得分:0)

SELECT company,CASE WHEN count_ > 1 THEN NULL ELSE code END
(
SELECT company,MAX(code) code,count(1) count_
(SELECT company,code,rank() OVER(PARTITION BY company ORDER BY count_ DESC) rn FROM
(
select
       company,code,count(1) count_
from   table
group by
       company,code
) 
)
where rn = 1
GROUP BY 
   company
)