分類統(tǒng)計(jì)的時(shí)候,我們經(jīng)常會(huì)碰到這樣的需求,每個(gè)類按照一定順序,取幾條數(shù)據(jù),然后在一起顯示。這個(gè)問(wèn)題的解決方法,我們通過(guò)搜索引擎,可以找到很多種,但是不是SQL語(yǔ)句過(guò)于復(fù)雜,就是在數(shù)據(jù)量非常龐大的時(shí)候,性能就成了問(wèn)題。
數(shù)據(jù)結(jié)構(gòu):
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