sql over开窗函数
来源:互联网 发布:51单片机最小系统制作 编辑:程序博客网 时间:2024/04/29 14:28
2.在订单表(order)中统计中,生成这么每一条记录都包含如下信息:“所有订单的总和”、“每一位客户的所有订单的总和”、”每一单的金额“
关键点:使用了sum() over() 这个开窗函数
如图:
代码如下:
select customerID, SUM(totalPrice) over() as AllTotalPrice, SUM(totalPrice) over(partition by customerID) as cusTotalPrice, totalPrice from OP_Order
3.在订单表(order)中统计中,生成这么每一条记录都包含如下信息:“所有订单的总和(AllTotalPrice)”、“每一位客户的所有订单的总(cusTotalPrice)”、”每一单的金额(totalPrice)“,”每一个客户订单的平均金额(avgCusprice)“,”所有客户的所有订单的平均金额(avgTotalPrice)“,"客户所购的总额在所有的订单中总额的比例(CusAllPercent)","每一订单的金额在每一位客户总额中所占的比例(cusToPercent)"。
代码如下:
2 (
3 select
4 customerID,
5 SUM(totalPrice) over() as AllTotalPrice,
6 SUM(totalPrice) over(partition by customerID) as cusTotalPrice,
7 AVG(totalPrice) over(partition by customerID) as avgCusprice,
8 AVG(totalPrice) over() as avgTotalPrice,
9 totalPrice
10 from OP_Order
11 )
12 select
13 customerID,
14 AllTotalPrice,
15 cusTotalPrice,
16 totalPrice,
17 avgCusprice,
18 avgTotalPrice,
19 cusTotalPrice/AllTotalPrice as CusAllPercent,
20 totalPrice/cusTotalPrice as cusToPercent
21 from tabs
22
4.在订单表(order)中统计中,生成这么每一条记录都包含如下信息:“所有订单的总和(AllTotalPrice)”、“每一位客户的所有订单 的总(cusTotalPrice)”、”每一单的金额(totalPrice)“,”每一个客户订单的平均金额(avgCusprice)“,”所有客 户的所有订单的平均金额(avgTotalPrice)“,"订单金额最小值(MinTotalPrice)","客户订单金额最小值(MinCusPrice)","订单金额最大值(MaxTotalPrice)","客户订单金额最大值(MaxCusPrice)","客户所购的总额在所有的订单中总额的比例(CusAllPercent)","每一订单的金 额在每一位客户总额中所占的比例(cusToPercent)"。
关键:利用over子句进行操作。
如图:
具体代码如下:
2 (
3 select
4 customerID,
5 SUM(totalPrice) over() as AllTotalPrice,
6 SUM(totalPrice) over(partition by customerID) as cusTotalPrice,
7 AVG(totalPrice) over(partition by customerID) as avgCusprice,
8 AVG(totalPrice) over() as avgTotalPrice,
9 MIN(totalPrice) over() as MinTotalPrice,
10 MIN(totalPrice) over(partition by customerID) as MinCusPrice,
11 MAX(totalPrice) over() as MaxTotalPrice,
12 MAX(totalPrice) over(partition by customerID) as MaxCusPrice,
13 totalPrice
14 from OP_Order
15 )
16 select
17 customerID,
18 AllTotalPrice,
19 cusTotalPrice,
20 totalPrice,
21 avgCusprice,
22 avgTotalPrice,
23 MinTotalPrice,
24 MinCusPrice,
25 MaxTotalPrice,
26 MaxCusPrice,
27 cusTotalPrice/AllTotalPrice as CusAllPercent,
28 totalPrice/cusTotalPrice as cusToPercent
29 from tabs
30
总结:领用聚合函数再结合over子句,可以使表格向右扩张。并进行一些数据的统计。
- sql over开窗函数
- sql over开窗函数
- sql over开窗函数
- sql over开窗函数
- sql开窗函数over()用法
- sql开窗函数over()用法
- Sql server 开窗函数over()的语法
- Sql server 开窗函数over()的语法
- over 开窗函数使用说明
- oracle开窗函数over()
- Over子句开窗函数
- 理解over()开窗函数
- sql中的 开窗函数over() 聚合函数 排名函数
- sql over开窗函数 和group by的区别
- oracle over() 开窗函数介绍
- Oracle分析函数over及开窗函数
- 分析函数over 及开窗函数
- SQL--浅析,开窗函数,聚合开窗函数,排序开窗函数。
- Java Facade模式
- ubuntu tomcat6 自动启动 sysv-rc-conf 使用
- BurpSuite爆破密码
- 8.将构造函数和非成员函数“虚拟化”
- linux 关闭防火墙
- sql over开窗函数
- 散列表
- js中判断鼠标按键
- Android 启动另一个apk
- Poj 2299 Ultra-QuickSort
- JQuery JSON
- ognl.OgnlException: target is null for setProperty(null, "newsid", [Ljava.lan
- 在每一行的结束处使用CRLF
- C#设置文件夹为属性,编写的时候经常不注意的问题。