用rank对oracle分组排序
2005-11-16 10:46
495 查看
前些日子做了个数据分组后重新排序的报表。是用crystal脚本写的,先设置一个全局变量,让其自加,然后在每一个group head里面清零。代码很简单,我就不贴了。
过段日子,有同事求一个分组后重新排序的语句,他以为我的报表是用类似的sql做的。其实如果用oracle的话,rank函数也可以实现分组后重新排序。
RANK Function
The RANK function produces an ordered ranking of rows starting with a rank of one. Users specify an optional
PARTITION clause and a required ORDER BY clause. The PARTITION keyword is used to define where the rank
resets. The specific column which is ranked is determined by the ORDER BY clause. If no partition is specified,
ranking is performed over the entire result set. RANK will assign a rank of 1 to the smallest value unless descending
order is used. The following example ranks salesmen for each region based on their sales amount.
SELECT sales_person, sales_region, sales_amount,
RANK() OVER (PARTITION BY s_region ORDER BY s_amount DESC)
FROM Sales_table;
sales_person ,sales_region ,sales_amount, rank
Adams East 100 1
Baker East 99 2
Connors East 89 3
Davis East 75 4
Edwards West 74 1
Fitzhugh West 66 2
Gariabaldi West 45 3
过段日子,有同事求一个分组后重新排序的语句,他以为我的报表是用类似的sql做的。其实如果用oracle的话,rank函数也可以实现分组后重新排序。
RANK Function
The RANK function produces an ordered ranking of rows starting with a rank of one. Users specify an optional
PARTITION clause and a required ORDER BY clause. The PARTITION keyword is used to define where the rank
resets. The specific column which is ranked is determined by the ORDER BY clause. If no partition is specified,
ranking is performed over the entire result set. RANK will assign a rank of 1 to the smallest value unless descending
order is used. The following example ranks salesmen for each region based on their sales amount.
SELECT sales_person, sales_region, sales_amount,
RANK() OVER (PARTITION BY s_region ORDER BY s_amount DESC)
FROM Sales_table;
sales_person ,sales_region ,sales_amount, rank
Adams East 100 1
Baker East 99 2
Connors East 89 3
Davis East 75 4
Edwards West 74 1
Fitzhugh West 66 2
Gariabaldi West 45 3
相关文章推荐
- 【Oracle】day04_子查询_分页查询_DECODE_排序_集合操作_高级分组
- ORACLE 分组排序取每组的第一条记录
- oracle 根据某个字段分组,再根据另一个字段排序的sql查询 即 组内排序
- oracle 分组排序后字段序列值
- oracle下数据的排序分组row_number() over()--分析函数,可用于去重
- oracle中分组排序函数
- oracle小记1--分组 排序 取最新时间记录
- 关于oracle分组后组外排序的问题
- ORACLE-排序技巧ROW_NUM,ROW_NUMBER,RANK,DENSE_RANK,
- MySQL中实现Oracle里面 rank()over ( PARTITION BY ORDER BY) 分类分组功能
- oracle笔记(5)----排序函数row_number、rank、dense_rank
- sql_ROW_NUMBER() OVER(),partition by,rank(),dense_rank()分组以及排序
- MYSQL-实现ORACLE 和SQLserver数据中- row_number() over(partition by ) 分组排序功能
- oracle小记1--分组 排序 取最新时间记录
- Oracle 排序分析函数之ROW_NUMBER、RANK和DENSE_RANK
- Oracle-分析函数之排序值rank()和dense_rank()
- [Oracle SQL] 使用rollup分组统计按统计结果分组排序显示的问题
- Oracle 分组后,对组内数据进行排序
- PAT 1025. PAT Ranking (25)(分组排序再总体排序,计算rank)
- MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能