分類統計的時候,我們經常會碰到這樣的需求,每個類按照一定順序,取幾條數據,然后在一起顯示。這個問題的解決方法,我們通過搜索引擎,可以找到很多種,但是不是SQL語句過于復雜,就是在數據量非常龐大的時候,性能就成了問題。
數據結構:
create table tb(
count int not null,
type varchar(32) not null
)
解決方案:
select count, type from (select count, type, row_number() over(partition by type order type count desc) as rowindex from tb) t where rowindex <= 10