1

我一直在努力解决这个问题。我想我很接近了。(甲骨文,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,我会得到 (Google, A), (Google, B), (Google, C)。我什么都不想要。

我相信我可以再次加入整个事情,并加入一些额外的 where 子句来过滤掉重复的公司,但是我想知道是否有更好的方法来做到这一点。让我丧命的是聚合函数以及 group by,因为有时我会收到 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;

4

3 回答 3

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 (即只有一行有那个数量的记录。所以我们最终得到:

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 检查在顶部的情况下(因为我们不想过滤掉行,只需将它们标记为模棱两可。

于 2012-11-12T18:03:25.567 回答
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) 

是一个小提琴

于 2012-11-12T18:12:40.940 回答
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
)
于 2013-06-06T01:42:44.933 回答