惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
Hugging Face - Blog
Hugging Face - Blog
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
D
Docker
C
Cyber Attacks, Cyber Crime and Cyber Security
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
T
Tenable Blog
P
Privacy International News Feed
Google DeepMind News
Google DeepMind News
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
The Hacker News
The Hacker News
Project Zero
Project Zero
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
V
Visual Studio Blog
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
Jina AI
Jina AI
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Scott Helme
Scott Helme
A
About on SuperTechFans
WordPress大学
WordPress大学
F
Fortinet All Blogs
大猫的无限游戏
大猫的无限游戏
G
GRAHAM CLULEY
Latest news
Latest news
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Schneier on Security

博客园 - Sunlight

物化视图每天自动刷新2次的方法 Eclipse保存验证JS缓慢 Eclipse去除js错误 jquery动态添加删除行 收藏网址 Eclipse 修改项目名称 SQL 实现返回一个字段中,某个字符出现的次数 Oracle DIV/CSS 实现三列,左右两列固定,中间一列自适应 PowerDesigner15 操作记录 使用gzip优化web应用(filter实现) 增强我们的Visual Studio .Net 热调试 Axis 客户端调用 javascritp对fckeditor编辑器操作 - Sunlight - 博客园 Spring中任务调度cronExpression配置说明 - Sunlight - 博客园 基于 Axis 的WebService接口 SQL查询Excel 用 JavaScript 实现网页图片等比例缩放 漂亮的按钮样式
分析函数RANK的使用
Sunlight · 2009-03-16 · via 博客园 - Sunlight

有这么一个表table1

username  subject  score

张三     语文    80
张三     数学    90
张三     英语    88
李四     语文    90
李四     数学    92
李四     英语    94
王五     语文    62
王五     数学    96
王五     英语    76

现在要按科目排名次,得到如下结果:

username  subject  score  level

张三     语文    80   2
张三     数学    90   3
张三     英语    88   2
李四     语文    90   1
李四     数学    92   2
李四     英语    94   1
王五     语文    62   3
王五     数学    96   1
王五     英语    76   3
……

那么sql要这么写:select username,subject,score,RANK() OVER (PARTITION BY subject ORDER BY score desc) level from table1

只取每门科目头3名的记录:select * from (select username,subject,score,RANK() OVER (PARTITION BY subject ORDER BY score desc) level from table1) where level<=3

RANK() OVER (PARTITION BY subject ORDER BY score desc) 的意思是按照subject分组对score进行排序
和RANK类似的还有DENSE_RANK,只是遇到排名相同的情况,RANK有跳跃,DENSE_RANK没有跳跃

分析函数FIRST_VALUE、LAST_VALUE

FIRST_VALUE、LAST_VALUE是两个分析函数,返回结果集中排在第一位和最后一位的值。语法是:FIRST_VALUE (expr) OVER ( analytic_clause)
如:select t.*,first_value(username) over(partition BY organid order by userid asc) from tbuser t

select t.*,first_value(username) over(order by userid asc) from tbuser t