1.使用over子句与rows_number()以及聚合函数进行使用,可以进行编号以及各种操作。而且利用over子句的分组效率比group by子句的效率更高。
2.在订单表(order)中统计中,生成这么每一条记录都包含如下信息:“所有订单的总和”、“每一位客户的所有订单的总和”、”每一单的金额“
关键点:使用了sum() over() 这个开窗函数
如图:
代码如下:
1 select
2 customerID,
3 SUM(totalPrice) over() as AllTotalPrice,
4 SUM(totalPrice) over(partition by customerID) as cusTotalPrice,
5 totalPrice
6 from OP_Order
2 customerID,
3 SUM(totalPrice) over() as AllTotalPrice,
4 SUM(totalPrice) over(partition by customerID) as cusTotalPrice,
5 totalPrice
6 from OP_Order