在开发中遇到了一个需求:
有一张进度表ProgressMonthly 字段有 id, projectid, year, month, complete
目标是: 统计所有项目的最新月份的complete 数量
解决思路: 使用分析函数: ......OVER ( PARTITION BY ...... ORDER BY ...... ) 筛选出每个项目的最新月份的数据然后sum()
常用的分析函数有:
- row_number() over(partition by ... order by ...)
- rank() over(partition by ... order by ...)
- dense_rank() over(partition by ... order by ...)
- count() over(partition by ... order by ...)
- max() over(partition by ... order by ...)
- min() over(partition by ... order by ...)
- sum() over(partition by ... order by ...)
- avg() over(partition by ... order by ...)
- first_value() over(partition by ... order by ...)
- last_value() over(partition by ... order by ...)
- lag() over(partition by ... order by ...)