at
select * from orders;
--为什么要group by ??不需要
--当前月和全年的销售额
select month,
sum(tot_sales) month_sales,
sum(sum(tot_sales)) over(order by month rows between unbounded preceding and unbounded following) total_sales
from orders
group by month;
select month,
tot_sales month_sales,
sum(tot_sales) over() total_sales,
sum(tot_sales) over(partition by 1) total_sales2,
sum(tot_sales) over(order by month rows between unbounded preceding and unbounded following) total_sales3
from orders
--------------------------------------------漂亮的递规 rows/range 语句,显然只有order by的时候才有作用的
--每月递规的的销售额
select month,
tot_sales month_sales,
sum(tot_sales) over(order by month desc rows between 0 preceding and unbounded following) all_sales --order by是亮点
from orders
order by month;
select month,
tot_sales month_sales,
sum(tot_sales) over(order by month rows between unbounded preceding and 0 following) all_sales --order by是亮点
from orders
select month,
tot_sales month_sales,
sum(tot_sales) over(order by month RANGE BETWEEN UNBOUNDED preceding AND current row) all_sales --order by是亮点
from orders
SELECT b.month, SUM (a.tot_sales) month_sales
FROM orders a, orders b
WHERE a.month<= b.month
GROUP BY b.month;
select o.month,
(select sum(o1.tot_sales) from orders o1 where o1.month <= o.month)
from orders o
-------------------------------------------
---当前月,前一月,后一月的销售额
select month,
first_value(sum(tot_sales)) over (order by month
rows between 1 preceding and 1 following) prev_month,
sum(tot_sales) monthly_sales,
last_value(sum(tot_sales)) over (order by month
rows between 1 preceding and 1 following) next_month,
avg(sum(tot_sales)) over (order by month
rows between 1 preceding and 1 following) rolling_avg
from orders
group by month
order by month;
select month,
sum(tot_sales) over(order by month rows between 1 preceding and 0 following) - tot_sales pre_month, -- -1 to 0
tot_sales monthly_sales,
sum(tot_sales) over(order by month rows between 0 preceding and 1 following) - tot_sales next_month ,
avg(tot_sales) over (order by month
rows between 1 preceding and 1 following) rolling_avg -- -1 to 1
from orders;
select *
from orders ocurrent, orders onext
where onext.month(+) = ocurrent.month + 1
select ocurrent.month ,ocurrent.tot_sales ocurrent ,onext.tot_sales onext ,opre.tot_sales opre
from orders ocurrent
left join orders onext on onext.month - 1 = ocurrent.month
left join orders opre on opre.month +1 = ocurrent.month
order by ocurrent.month
-------------- 按照季度
select o.month,
(case
when o.month < '04' then
'01'
when o.month < '07' then
'02'
when o.month < '10' then
'03'
else
'04'
end) q,
o.tot_sales,
sum(tot_sales) over(partition by(case
when o.month < '04' then
'01'
when o.month < '07' then
'02'
when o.month < '10' then
'03'
else
'04'
end))
from orders o
-------------------
--有多少个月份的sales和当月的sales的差距是在-10000 to 10000之间
select month,
tot_sales month_sales,
count(1) over(order by tot_sales RANGE BETWEEN 10000 preceding AND 10000 following) count_Num --order by是亮点
from orders o
order by o.month
分析函数的关键特征是它们允许结果集的单行可以包括计算的多行交叉而不需要在整个结果集上处理GROUP BY。其允许多行和单行数据在一个select语句中而不需要部署子查询。
分析函数的语法
FUNCTION() OVER (<partition by> < <order by> <windowing clause> >)
PARTITION BY定义函数将处理的组,如果partition by被忽略,那么整个select返回的数据集将作为一个活动分区。
ORDER BY排序分区内的数据,可以声明ASC/DESC,以及NULLS FIRST和NULLS LAST标识null的处理。
WINDOWING CLAUSE不能在每个分区函数上使用,如果可用,必须和ORDER BY一起使用。窗口子句声明了将在函数中计算的当前行周围的行集。窗口可以定义为ROWS或RANGE,ROWS相对于活动分区中的行数,而RANGE则查看包含在这些行中的值。默认为ROWS UNBOUNDED PROCEDING事实上包含了所有行。
分析函数的列表
分析函数通常分为三类,数组,聚集,其他。
最常用的函数包括:
函数
|
类型
|
Partition by
|
Order By
|
Window
|
DENSE_RANK
|
数组
|
可选
|
必须
|
--
|
RANK
|
数组
|
可选
|
必须
|
--
|
ROW_NUMBER
|
数组
|
可选
|
必须
|
--
|
NTILE
|
数组
|
可选
|
必须
|
--
|
AVG
|
聚集
|
可选
|
可选
|
可选
|
COUNT
|
聚集
|
可选
|
可选
|
可选
|
SUM
|
聚集
|
可选
|
可选
|
可选
|
RATIO_TO_REPORT
|
聚集
|
可选
|
--
|
--
|
FIRST_VALUE
|
其他
|
可选
|
可选
|
可选
|
LAST_VALUE
|
其他
|
可选
|
可选
|
可选
|
LAG
|
其他
|
可选
|
必须
|
--
|
LEAD
|
其他
|
可选
|
必须
|
--
|
MAX
|
其他
|
可选
|
可选
|
可选
|
MIN
|
其他
|
可选
|
可选
|
可选
|
分享到:
相关推荐
两种AT蓝牙指令发送和解析,一种是逐步分析,一种直接函数指针操作,都可以大量复制。 都是商业应用之后的代码截取。保证可以使用 。
描述了AT91sam9260系列中断的详细步骤,从中断触发-》查找中断号-》找到中断处理函数
基于 AT89C51 单片机的多功能函数信号发生器设计 本文是基于 AT89C51 单片机的多功能函数信号发生器设计的毕业论文。该设计的主要目的是为了设计一个多功能的信号发生器,可以生成多种波形,如三角波、锯齿波、矩形...
这些性质都是冲激函数非常重要的性质,它们可以帮助我们更好地分析和处理信号。 奇异函数是信号处理和系统分析中非常重要的一类函数。它们具有特殊的性质,可以帮助我们更好地分析和处理信号。本文中,我们讨论了...
很实用的时频分析工具箱,包括短时傅里叶正反变换等,很好,很强大哦~~分享给大家,包括以下函数的源代码: % sigmerge - Add two signals with given energy ratio in dB. % % Choice of the Instantaneous ...
本文档主要描述SAM6260平台消息处理接口、AT命令接口及入口函数处理过程、AT命令添加方法。
如果用计数就可以得到比较精确的频率,就将这个频率值直接送入显示译码部 分。 (6)显示译码 将测量值转换成七段译码数据,送入显示电路。 (7)显示电路 通过4 个LED 数码管将测得的频率值显示给用户。 (8)系统软件 ...
本次演示介绍了一种基于AT89C52单片机的智能温控器,通过深入分析其原理、硬件设计和软件设计过程,实现了对环境温度的实时监测和自动调节。展望未来,可以进一步研究智能温控器的智能化程度和能效优化等方面的提升...
主要介绍了python中使用zip函数出现错误的原因分析及解决方法,需要的朋友可以参考下
COMSOL是一个功能强大的有限元分析软件,提供了多种内置函数来帮助用户实现复杂的模拟和分析。本文档总结了COMSOL内置函数的定义和使用方法,涵盖了微分算符、导数计算、边界处理、弱形式、Jacobian矩阵计算、邻近...
复制战士Copyfighter静态分析Go代码并报告按值传递大型结构的函数。 它可以帮助您帮助您的代码。 每个Go函数调用都会复制为其提供的值,包括结构。 如果在不使用指向大型结构的指针的情况下传递大型结构,则在内存中...
这些函数能通过用户自定义函数进一步扩展。M AT L A B有许多强有力的命令。例如, M AT L A B能够用一个单一的命令求解线性系统,能完成大量的高级矩阵处理。M AT L A B有强有力的二维、三维图形工具。M AT L A B能...
首先,看一下python lambda函数的语法,如下: f=lambda [parameter1,parameter2,……]:expression lambda语句中,冒号前是参数,可以有0个或多个,用逗号隔开,冒号右边是返回值。lambda语句构建的其实是一个函数...
这些函数能通过 用户自定义函数进一步扩展。 M AT L A B有许多强有力的命令。例如, M AT L A B能够用一个单一的命令求解线性系统, 能完成大量的高级矩阵处理。 M AT L A B有强有力的二维、三维图形工具。 M AT L A...
这些函数能通过 用户自定义函数进一步扩展。 M AT L A B有许多强有力的命令。例如, M AT L A B能够用一个单一的命令求解线性系统 能完成大量的高级矩阵处理。 M AT L A B有强有力的二维、三维图形工具。 M AT L A B...
这些函数能通过 用户自定义函数进一步扩展。 M AT L A B有许多强有力的命令。例如, M AT L A B能够用一个单一的命令求解线性系统, 能完成大量的高级矩阵处理。 M AT L A B有强有力的二维、三维图形工具。 M AT L A...
这些函数能通过 用户自定义函数进一步扩展。 M AT L A B有许多强有力的命令。例如, M AT L A B能够用一个单一的命令求解线性系统, 能完成大量的高级矩阵处理。 M AT L A B有强有力的二维、三维图形工具。 M AT L A...
主成分分析算法的MATLAB代码实现,可以帮助理解主成分分析算法的计算过程,比直接调用MATLAB函数更利于学习。
本文实例讲述了python匿名函数用法。分享给大家供大家参考,具体如下: 匿名函数特点: 1.只能有一个表达式 2.不用return,默认return结果 3.不需要名字,防止函数名重复 4.是对象,可赋值给一个变量 经典例子 >>> ...