語法:row_number() over (partition by 字段a order by 計算項b desc ) rank
--這里rank是別名
partition by:類似hive的建表,分區的意思;
order by :排序,默認是升序,加desc降序;
這里按字段a分區,對計算項b進行降序排序
實例:
要取top10品牌,各品牌的top10渠道,各品牌的top10渠道中各渠道的top10檔期
1、取top10品牌
select 品牌,count/sum/其它() as num from table_name order by num limit 10;
2、 取top10品牌下各品牌的top10渠道
select
a.*
from
(
select 品牌,渠道,count/sum/其它() as num row_number() over (partition by 品牌 order by num desc ) rank
from table_name
where 品牌限制條件
group by 品牌,渠道
)a
where
a.rank<=10
3、 取top10品牌下各品牌的top10渠道中各渠道的top10檔期
select
a.*
from
(
select 品牌,渠道,檔期,count/sum/其它() as num row_number() over (partition by 品牌,渠道 order by num desc ) rank
from table_name
where 品牌,渠道 限制條件
group by 品牌,渠道,檔期
)a
where
a.rank<=10