SQL选择最大值(计数(A列、B列))
我一直在为这件事伤脑筋。我想我很接近了。(Oracle,SQL) 我有一张像下面这样的桌子SQL选择最大值(计数(A列、B列)),sql,oracle,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语句,对于每个公司,它将为我提供出现频率最高的代码。所以在我的例子中,我得到 Appl
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
mj您可以通过以下方式完成:
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
给予
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(即只有一行具有该数量的REC)。因此,我们最终得出:
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
ie 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 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
给予
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(即只有一行具有该数量的REC)。因此,我们最终得出:
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
ie rn=1,c>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)
是小提琴试试看
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)
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
)
是fiddleOracle数据库11g企业版11.2.0.1.0-64位产品Oracle数据库11g企业版11.2.0.1.0-64位产品
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
)