百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 优雅编程 > 正文

sql语法之over用法(sql overlay)

sinye56 2024-09-22 08:26 6 浏览 0 评论

/***Sql Server*/

/*

OVER ( [ <PARTITION BY clause> ]

[ <ORDER BY clause> ]

[ <ROW or RANGE clause> ]

)

<PARTITION BY clause> ::= PARTITION BY value_expression , ... [ n ]

--PARTITION BY将查询结果集分为多个分区,开窗函数分别应用于每个分区,并为每个分区重新启动计算;

-- 如果未指定 PARTITION BY,则此函数将查询结果集的所有行视为单个组。

-- value_expressio:指定行集按其分区的列。value_expression 只能引用可供 FROM 子句使用的列。

-- value_expression 可以是列表达式、标量子查询、标量函数或用户定义的变量

<ORDER BY clause> ::= ORDER BY order_by_expression [ COLLATE collation_name ] [ ASC | DESC ] [ ,...n ]

--ORDER BY定义结果集的每个分区中行的逻辑顺序。也就是说,它指定按其执行开窗函数计算的逻辑顺序。

-- 如果未指定 ROWS/RANGE,但指定了 ORDER BY,则将 RANGE UNBOUNDED PRECEDING AND CURRENT ROW 用作窗口的默认值。

-- order_by_expression指定用于进行排序的列或表达式,只能引用可供 FROM 子句使用的列,不能将整数指定为表示列名或别名。

<ROW or RANGE clause> ::= { ROWS | RANGE } <window frame extent>

<window frame extent> ::= { <window frame preceding> | <window frame between> }

<window frame between> ::= BETWEEN <window frame bound> AND <window frame bound>

<window frame bound> ::= { <window frame preceding> | <window frame following> }

<window frame preceding> ::= { UNBOUNDED PRECEDING | <unsigned_value_specification> PRECEDING | CURRENT ROW }

<window frame following> ::= { UNBOUNDED FOLLOWING | <unsigned_value_specification> FOLLOWING | CURRENT ROW }

<unsigned value specification> ::= { <unsigned integer literal> }

--Range用于指定order by列表中的值与指定的行一致的记录行,这个指定的行只能是current row(当前行)或unbounded preceding(分区内的第一行) 。

--Rows是用于指定某一行到另外一行之间的所有行,其中的“某一行”和"另外一行"均可以是"n preceding(当前行之前的第n行)"、

-- "n following(当前行之后的第n行)"、"current row(当前行)"、"unbounded preceding(分区内的第一行)"、

-- "unbounded following(分区内的最后一行)";

-- 但是要求在当前窗口中“某一行”必须排在"另外一行"之前。

-- Rows一般与between...and...同时出现,即“Rows between 某一行 and另外一行”;

-- 但是between..and也可以省略,表示从指定行到当前行之间的行,其中指定行必须为当前行之前的行,

-- 即"unbounded preceding(分区内的第一行)"或"n preceding(当前行之前的第n行)",比如 Rows unbounded preceding 。

*/

/*创建测试表*/

create table hptestover

(

v varchar(10),

rn int

);

insert into hptestover(v,rn)values('a',1),('a',2),('a',3);

insert into hptestover(v,rn)values('b',1),('b',2),('b',3);

insert into hptestover(v,rn)values('a',5);

insert into hptestover(v,rn)values('a',4);

/*进行查询分析:RANGE 只支持使用 UNBOUNDED 和 CURRENT ROW 窗口框架分隔符*/

select v,rn,

sum(rn) over(partition by v order by rn rows between 1 preceding and current row ) row_p1, /*将本行和上一行的rn值相加*/

--sum(rn) over(partition by v order by rn range between 1 preceding and current row ) range_p1, /*将本行和上一行的rn值相加*/

sum(rn) over(partition by v order by rn rows 1 preceding) row_p1_1 , /*将本行和上一行的rn值相加,省略了between,因此,oracle认为结束行时当前行,而开始行为1 precding*/

--sum(rn) over(partition by v order by rn range 1 preceding) range_p1_1 , --将本行和上一行的rn值相加,省略了between,因此,oracle认为结束行时当前行,而开始行为1 precding*/

sum(rn) over(partition by v order by rn rows between current row and 1 following) row_f1, /*将本行和下一行的rn值相加*/

--sum(rn) over(partition by v order by rn range between current row and 1 following) range_f1, /*将本行和下一行的rn值相加*/

sum(rn) over(partition by v order by rn rows between unbounded preceding and current row) row_pn, /*将第一行到当前行的所有rn值相加*/

sum(rn) over(partition by v order by rn range between unbounded preceding and current row) range_pn, /*将第一行到当前行的所有rn值相加*/

sum(rn) over(partition by v order by rn rows unbounded preceding ) row_pn_1, /*将第一行到当前行的所有rn值相加,省略between*/

sum(rn) over(partition by v order by rn range unbounded preceding ) range_pn_1,/*将第一行到当前行的所有rn值相加,省略between*/

sum(rn) over(partition by v order by rn rows between current row and unbounded following) row_fn,/*将当前行到最后一行的所有rn值相加*/

sum(rn) over(partition by v order by rn range between current row and unbounded following) range_fn,/*将当前行到最后一行的所有rn值相加*/

sum(rn) over(partition by v order by rn rows between 1 preceding and 1 following) row_p1_f1, /*将当前行、前一行、最后一行的所有rn值相加*/

--sum(rn) over(partition by v order by rn range between 1 preceding and 1 following) range_p1_f1, /*将当前行、前一行、最后一行的所有rn值相加*/

sum(rn) over(partition by v order by rn rows between unbounded preceding and unbounded following) row_all, /*将所有v值相同的行相加*/

sum(rn) over(partition by v order by rn range between unbounded preceding and unbounded following) range_all, /*将所有v值相同的行相加*/

sum(rn) over(partition by v ) s_all /*将所有v值相同的行相加*/

from hptestover

order by v,rn;

/*清理测试表*/

drop table hptestover;

/***Oracle*/

/* 语法:

over(

[query_partition_clause]

[order by clause]

[windowing clause]

)

<query_partition_clause> ::= parition by expr,...[n]

<order by clause> ::= order[sliblings] by {expr|position|c_alias} [asc|desc] [nulls first|nulls last] [,...n]

<windowing_clause> ::= {rows|range} <window frame extent>

<window frame extent> ::= { <window frame between> | <window frame preceding> }

<window frame between> ::= BETWEEN <window frame bound1> AND <window frame bound2>

--BETWEEN...AND子句为窗口指定开始和结束位置,第一个表达式定义开始位置,第二个表达式定义结束位置,

-- 如果省略了bewteen,oracle会认为结束位置是当前行,而开始位置是表达式指定的行;

<window frame bound1> ::= { UNBOUNDED PRECEDING | CURRENT ROW | value_expr {PRECEDING|FOLLOWING} }

<window frame bound2> ::= { UNBOUNDED FOLLOWING | CURRENT ROW | <value_expr> {PRECEDING|FOLLOWING} }

<window frame preceding> ::= { UNBOUNDED PRECEDING | CURRENT ROW | <value_expr> PRECEDING }

<value_expr> ::= {columns|nonanalytic functions|function expressions|expressions involving any of these}

ROWS和RANGE关键字为每行定义一个窗口(物理或逻辑的行的集合),用于计算函数的结果,分析函数可以应用到在窗口中的所有行,按自上而下的顺序计算函数值。

ROWS 标识出窗口的物理单元(rows)。 ROWS specifies the window in physical units (rows)

RANGE 通过逻辑偏移量标识出窗口。RANGE specifies the window as a logical offset. 指的是order by的值的差额的范围

-- RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 可简写为 RANGE UNBOUNDED PRECEDING.

-- RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING

-- RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 可简写为 RANGE UNBOUNDED PRECEDING.

-- RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING

*/

create table hptestover

(

v varchar(10),

v1 int,

rn int

);

insert into hptestover(v,v1,rn)

select 'a',level,level from dual connect by level < 7;

insert into hptestover(v,v1,rn)

select 'b',level*level,level from dual connect by level < 4;

insert into hptestover(v,v1,rn)values('c',1,5);

insert into hptestover(v,v1,rn)values('c',3,6);

insert into hptestover(v,v1,rn)values('c',9,10);

insert into hptestover(v,v1,rn)values('d',1,2);

insert into hptestover(v,v1,rn)values('d',2,4);

insert into hptestover(v,v1,rn)values('d',4,7);

/*RANGE只指定了前后两个值(order by列的值)之间相差值的范围,而ROWS则指定了前后多少行的范围。*/

/*进行查询分析*/

select v,v1,rn,

sum(rn) over(partition by v order by v1 rows between 1 preceding and current row ) row_p1, /*将本行和上一行的rn值相加*/

sum(rn) over(partition by v order by v1 range between 1 preceding and current row ) range_p1, /*将本行之前的rn差额不超过1,本行之后的的rn等于当前行的所有rn值相加*/

sum(rn) over(partition by v order by v1 range between 2 preceding and current row ) range_p2, /*将本行之前的rn差额不超过2,本行之后的的rn等于当前行的所有rn值相加*/

sum(rn) over(partition by v order by v1 rows 1 preceding) row_p1_1 , /*将本行和上一行的rn值相加,省略了between,因此,oracle认为结束行时当前行,而开始行为1 precding*/

sum(rn) over(partition by v order by v1 range 1 preceding) range_p1_1 , /*将本行之前的rn差额不超过1,本行之后的的rn等于当前行的所有rn值相加,省略了between,因此,oracle认为结束行时当前行,而开始行为1 precding*/

sum(rn) over(partition by v order by v1 range 2 preceding) range_p2_1 , /*将本行之前的rn差额不超过2,本行之后的的rn等于当前行的所有rn值相加*/

sum(rn) over(partition by v order by v1 range 4 preceding) range_p4_1 , /*将本行之前的rn差额不超过2,本行之后的的rn等于当前行的所有rn值相加*/

sum(rn) over(partition by v order by v1 rows between current row and 1 following) row_f1, /*将本行和下一行的rn值相加*/

sum(rn) over(partition by v order by v1 range between current row and 1 following) range_f1, /*将本行和下一行的rn值相加*/

sum(rn) over(partition by v order by v1 range between current row and 4 following) range_f4, /*将本行和下一行的rn值相加*/

sum(rn) over(partition by v order by v1 rows between unbounded preceding and current row) row_pn, /*将第一行到当前行的所有rn值相加*/

sum(rn) over(partition by v order by v1 range between unbounded preceding and current row) range_pn, /*将第一行到当前行的所有rn值相加*/

sum(rn) over(partition by v order by v1 rows unbounded preceding ) row_pn_1, /*将第一行到当前行的所有rn值相加,省略between*/

sum(rn) over(partition by v order by v1 range unbounded preceding ) range_pn_1, /*将第一行到当前行的所有rn值相加,省略between*/

sum(rn) over(partition by v order by v1 rows between current row and unbounded following) row_fn, /*将当前行到最后一行的所有rn值相加*/

sum(rn) over(partition by v order by v1 range between current row and unbounded following) range_fn, /*将当前行到最后一行的所有rn值相加*/

sum(rn) over(partition by v order by v1 rows between 1 preceding and 1 following) row_p1_f1, /*将当前行、前一行、最后一行的所有rn值相加*/

sum(rn) over(partition by v order by v1 range between 1 preceding and 1 following) range_p1_f1, /*将当前行、前一行、最后一行的所有rn值相加*/

sum(rn) over(partition by v order by v1 rows between unbounded preceding and unbounded following) row_all, /*将所有v值相同的行相加*/

sum(rn) over(partition by v order by v1 range between unbounded preceding and unbounded following) range_all, /*将所有v值相同的行相加*/

sum(rn) over(partition by v ) s_all /*将所有v值相同的行相加*/

from hptestover

order by v,v1;

--清理测试表

drop table hptestover;

相关推荐

RHEL8和CentOS8怎么重启网络

本文主要讲解如何重启RHEL8或者CentOS8网络以及如何解决RHEL8和CentOS8系统的网络管理服务报错,当我们安装好RHEL8或者CentOS8,重启启动网络时,会出现以下报错:...

Linux 内、外网双网卡路由配置

1.路由信息的影响Linux系统中如果有多张网卡的情况下,如果路由信息配置不正确,...

Linux——centos7修改网卡名

修改网卡名这个操作可能平时用不太上,可作为了解。修改网卡默认名从ens33改成eth01.首先修改网卡配置文件名(建议将原配置文件进行备份)...

CentOS7下修改网卡名称为ethX的操作方法

?Linux操作系统的网卡设备的传统命名方式是eth0、eth1、eth2等,而CentOS7提供了不同的命名规则,默认是基于固件、拓扑、位置信息来分配。这样做的优点是命名全自动的、可预知的...

Linux 网卡名称enss33修改为eth0

一、CentOS修改/etc/sysconfig/grub文件(修改前先备份)为GRUB_CMDLINE_LINUX变量增加2个参数(net.ifnames=0biosdevname=0),修改完成...

CentOS下双网卡绑定,实现带宽飞速

方式一1.新建/etc/sysconfig/network-scripts/ifcfg-bond0文件DEVICE=bond0IPADDR=191.3.60.1NETMASK=255.255.2...

linux 双网卡双网段设置路由转发

背景网络情况linux双网卡:网卡A(ens3)和网卡B(...

Linux-VMware设置网卡保持激活

Linux系统只有在激活网卡的状态下才能去连接网络,进行网络通讯。修改配置文件(永久激活网卡)...

VMware虚拟机三种网络模式

01.VMware虚拟机三种网络模式由于linux目前很热门,越来越多的人在学习linux,但是买一台服务放家里来学习,实在是很浪费。那么如何解决这个问题?虚拟机软件是很好的选择,常用的虚拟机软件有v...

Rocky Linux 9/CentOS Stream 9修改网卡配置/自动修改主机名(实操)

推荐...

2023年最新版 linux克隆虚拟机 解决网卡uuid重复问题

问题描述1、克隆了虚拟机,两台虚拟机里面的ip以及网卡的uuid都是一样的2、ip好改,但是uuid如何改呢?解决问题1、每台主机应该保证网卡的UUID是唯一的,避免后面网络通信有问题...

Linux网卡的Vlan配置,你可能不了解的玩法

如果服务器上连的交换机端口已经预先设置了TRUNK,并允许特定的VLAN可以通过,那么服务器的网卡在配置时就必须指定所属的VLAN,否则就不通了,这种情形在虚拟化部署时较常见。例如在一个办公环境中,办...

Centos7 网卡绑定

1、切换到指定目录#备份网卡数据cd/etc/sysconfig/network-scriptscpifcfg-enp5s0f0ifcfg-enp5s0f0.bak...

Linux搭建nginx+keepalived 高可用(主备+双主模式)

一:keepalived简介反向代理及负载均衡参考:...

Linux下Route 路由指令使用详解

linuxroute命令用于显示和操作IP路由表。要实现两个不同子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。在Linux系统中,设置路由通常是为了解决以下问题:该...

取消回复欢迎 发表评论: