-- 排序函数
使用函数及语法:
-- 1)先把各个用户所购买商品涉及的品类数给统计出来,时间为 2020年1月
-- 2) 在1的基础上排名,使count()用到排名窗口函数
SELECT
user_name,
COUNT( DISTINCT goods_category ),
row_number() over ( ORDER BY COUNT( DISTINCT goods_category ) ) AS '排序1',
rank() over ( ORDER BY COUNT( DISTINCT goods_category ) ) AS '排序2',
dense_rank() over ( ORDER BY COUNT( DISTINCT goods_category ) ) AS '排序3'
FROM
user_trade
WHERE
SUBSTRING( pay_time, 1, 7 )= '2020-01'
GROUP BY
user_name;
-- 使用ntile(n) over(...)----- n是指你切成几片
-- step:查询出将2020年2月的支付用户,按照支付金额分成5组后的结果
SELECT
user_name,
SUM( pay_amount ) pay_amount,
ntile( 5 ) over ( ORDER BY SUM( pay_amount ) DESC ) LEVEL
FROM
user_trade
WHERE
SUBSTRING( pay_time, 1, 7 )= '2020-02'
GROUP BY
user_name;
-- 查询出2020年支付金额排名前30%的所有用户
SELECT
a.user_name,
a.pay_amount,
a.LEVEL
FROM
(SELECT
user_name,
SUM( pay_amount ) pay_amount,
ntile( 10 ) over ( ORDER BY SUM( pay_amount ) DESC ) LEVEL
FROM
user_trade
WHERE
YEAR ( pay_time )= 2020
GROUP BY
user_name)a
WHERE
a.LEVEL IN (1, 2, 3);
知识点总结
这三个函数的作用都是返回相应规则的排序序号
1.row_number() over(partition by ...A... order by ...B... )
2.rank() over(partition by ...A... order by ...B... )
3.dense_rank() over(partition by ...A... order by ...B... )
A:分组的字段名称
B:排序的字段名称
注意:row_number()、rank() 和dense_rank()紧邻的括号内是不加任何字段名称的。
4. ntile(n) over(partition by ...A... order by ...B... )
n:切分的片数
A:分组的字段名称
B:排序的字段名称
ntile(n),用于将分组数据按照顺序切分成n片,返回当前切片值;NTILE不支持ROWS BETWEEN,