Oracle 11g:
我希望结果按最高计数列出,然后是ch_id。当我使用 group by 来获取计数时,我会忽略细节的粒度。我可以使用分析函数吗?
SALES
ch_id desc customer
=========================
ANAR Anari BOB
SWIS Swiss JOE
SWIS Swiss AMY
BRUN Brunost SAM
BRUN Brunost ANN
BRUN Brunost ROB
Desired Results
count ch_id customer
===========================================
3 BRUN ANN
3 BRUN ROB
3 BRUN SAM
2 SWIS AMY
2 SWIS JOE
1 ANAR BOB
答案 0 :(得分:1)
使用分析计数(*):
select * from
(
select count(*) over (partition by ch_id) cnt,
ch_id, customer
from sales
)
order by cnt desc
答案 1 :(得分:1)
select total, ch_id, customer
from sales s
inner join (select count(*) total, ch_id from sales group by ch_id) b
on b.ch_id = s.chi_id
order by total, ch_id
好的 - 使用分区同时发生的另一篇文章是Oracle的更好解决方案。但无论数据库如何,这个都有效。