- sql自学笔记(二十)——MySQL8.0版本的新特性(十)
- sql自学笔记(二十八)——MySQL8.0版本的新特性(十八)
- sql自学笔记(二十七)——MySQL8.0版本的新特性(十七)
- sql自学笔记(二十一)——MySQL8.0版本的新特性(十一)
- sql自学笔记(二十六)——MySQL8.0版本的新特性(十六)
- sql自学笔记(二十四)——MySQL8.0版本的新特性(十四)
- sql自学笔记(二十三)——MySQL8.0版本的新特性(十三)
- sql自学笔记(二十五)——MySQL8.0版本的新特性(十五)
- sql自学笔记(二十九)——MySQL8.0版本的新特性(十九)
- sql自学笔记(二十二)——MySQL8.0版本的新特性(十二)
窗口定义
window _ fun(tion cexpr)
OVER(
PAR TITION BY ... //对数据先进行分组
ORDER BY ... //分组之后的数据进行排序
frame _ clause ... //它是进一步来限定窗口
)
这些选项的效果


CURRENT ROW它表示我们当前要处理的这一行数据
M PRECEDING表示当前行的往上M行
N FOLLDAING表示当前行的往下N行
UNBOUNDED PRECEDING表示分区的第一行
UNBOUNDED FOLLOWING表示分区的最后一行
示例:我们仍然使用销售表
select * from sales;

我们想要计算一下从数据累计到我当前它的一个总和
select year,country,product,proft,
-> sum(profit) over (partition by country order by profit ros unbounded _ preceding) as runing _ total //按照国家进行分区,按照利润进行排序
-> from sales
-> order by country. profit;

还可以用来计算从开始到当前数据的移动平均值
select year,country,product,proft,
-> avg(profit) over (partition by country order by profit ros between 1 perceding and 1 following ) as runing _ avg
-> from sales
-> order by country. profit;

窗口定义的另外一种写法
select year,country,product,profit,
->first _ value(profit) over w as 'first' ,
->last _ value(profit) over w as 'last'
->from sales
->window w as (partition by country order by profit rows unbounded preceding)
->order by country,profit;

网友评论