下面查询获取2003年不同产品类型的销售评级
select prd_type_id,
sum(amount),
rank() over (order by sum(amount) desc) as rank,
dense_rank() over (order by sum(amount) desc) as dense_rank
from all_sales
where year=2003
and amount is not null
group by prd_type_id
order by prd_type_id
结果显示如下
1 1 905081.84 1 1
2 2 186381.22 4 4
3 3 478270.91 2 2
4 4 402751.16 3 3
在使用分析函数的时候,可以用NULLS FIRST和NULLS LAST显示地控制空值是一个分组中的最高还是最低的排名
select prd_type_id,
sum(amount),
rank() over (order by sum(amount) desc NULLS LAST) as rank,
dense_rank() over (order by sum(amount) desc NULLS LAST) as dense_rank
from all_sales
where year=2003
group by prd_type_id
order by prd_type_id
阅读(706) | 评论(0) | 转发(0) |