窗口函数 SELECT - OVER Clause (Transact-SQL)
https://docs.microsoft.com/en-us/sql/t-sql/queries/select-over-clause-transact-sql
Determines the partitioning and ordering of a rowset before the associated window function is applied.
That is, the OVER clause defines a window or user-specified set of rows within a query result set.
A window function then computes a value for each row in the window.
You can use the OVER clause with functions to compute aggregated values such as moving averages, cumulative累积的 aggregates, running totals, or a top N per group results.
分组
PARTITION BY
Divides the query result set into partitions.
The
window function is applied to each partition separately and computation
restarts for each partition.
根据什么进行分组
alue_expression
Specifies the column by which the rowset is partitioned.
value_expression can only refer to columns made available by the FROM clause.
value_expression cannot refer to expressions or aliases in the select list.
value_expression can be a column expression, scalar subquery, scalar function, or user-defined variable.
<ORDER BY clause>
Defines the logical order of the rows within each partition of the
result set.
That is, it specifies the logical order in which the window
functioncalculation is performed.
order_by_expression
Specifies a column or expression on which to sort.
order_by_expression
can only refer to columns made available by the FROM clause.
An integer
cannot be specified to represent a column name or alias.
sql server 2012以上的版本才支持
WITH temp2
AS ( SELECT Id ,
[number] ,
ROW_NUMBER() OVER ( ORDER BY ( SELECT 1 )) AS SNO
FROM dbo.TestPartition )
SELECT temp2.Id ,
temp2.number ,
SUM(number) OVER ( PARTITION BY temp2.Id
ORDER BY temp2.SNO ) AS 'number 累计值'
FROM temp2
sql server 2008以及以下的版本,使用下面的
/*
SQL Server 2005 以及 2008 做法
1.先分区,并编号*/
;
WITH cte
AS ( SELECT Id ,
number ,
ROW_NUMBER() OVER ( PARTITION BY Id
ORDER BY Id ) AS rnm
FROM dbo.TestPartition )
--调试语句
--SELECT * FROM cte
/*
2.再累加。用到了嵌套子查询
*/
SELECT Id ,
number ,
( SELECT SUM(number)
FROM cte t1
WHERE t1.rnm <= t2.rnm
AND --
t1.Id = t2.Id --保证是同一个人的数据在做累加
) AS 'number 累计值'
FROM cte t2;
窗口函数 SELECT - OVER Clause (Transact-SQL)的更多相关文章
- A Beginner’s Guide to the OUTPUT Clause in SQL Server
原文 A Beginner’s Guide to the OUTPUT Clause in SQL Server T-SQL supports the OUTPUT clause after the ...
- SQL-49 针对库中的所有表生成select count(*)对应的SQL语句
题目描述 针对库中的所有表生成select count(*)对应的SQL语句CREATE TABLE `employees` (`emp_no` int(11) NOT NULL,`birth_dat ...
- oracle(sql)基础篇系列(一)——基础select语句、常用sql函数、组函数、分组函数
花点时间整理下sql基础,温故而知新.文章的demo来自oracle自带的dept,emp,salgrade三张表.解锁scott用户,使用scott用户登录就可以看到自带的表. #使用ora ...
- Ora中select某时间段记录sql语句
要查找某时间段的记录,例如查找2013-11-1到2013-11-30的记录. ' group by user_name, user_id 注意:SQL语句中含有sum累加函数,末尾要加group b ...
- MySQL查询order by相减select相减的Sql语句
先看一张表 create_time是订单创建时间,pay_time是支付时间 现在我想按照订单完成耗时的时间进行排序,并且取出来的数据中直接算好了差值,怎么用Sql呢,请看 select id,tid ...
- oracle(sql)基础篇系列(一)——基础select语句、常用sql函数、组函数、分组函数
花点时间整理下sql基础,温故而知新.文章的demo来自oracle自带的dept,emp,salgrade三张表.解锁scott用户,使用scott用户登录就可以看到自带的表. #使用oracle用 ...
- JS中Float类型加减乘除 修复 JQ 操作 radio、checkbox 、select LINQ to SQL:Where、Select/Distinct LINQ to SQL Count/Sum/Min/Max/Avg Join
JS中Float类型加减乘除 修复 MXS&Vincene ─╄OvЁ &0000027─╄OvЁ MXS&Vincene MXS&Vincene ─╄Ov ...
- select into outfile的sql语句
SELECT INTO…OUTFILE语句把表数据导出到一个文本文件中,并用LOAD DATA …INFILE语句恢复数据.但是这种方法只能导出或导入数据的内容,不包括表的结构,如果 ...
- Oracle 11g 、 Oracle 11g select 、 PLSQL 、 Sql Server迁移助手(SSMA)6.0/7.1 网盘下载地址
- - - - - - - - 链接: https://pan.baidu.com/s/1q-uwAfeLOPxzBBx6V1pYLg 提取码: hei9
随机推荐
- spring 415
不支持的媒体类型 spring mvc 使用@requestBody注解json请求时,jQuery有限制,否则会出现 415 错误 1.使用ajax $.ajax({ ...
- Linux监控实时log
https://jingyan.baidu.com/article/93f9803f5545a3e0e46f5596.html
- java网络
title: java 网络 date: 2017年3月11日11:14:52 1. 复杂的东西就把他封装成对象 概述:(网络就是找到别人) 找到对方的机器,(找到对方的ip地址) 每个机器中有很多进 ...
- rxswift-self.usernameTF.rx.text.orEmpty.map
self.usernameTF.rx.text.orEmpty.map 一堆类型转化+数据处理的操作 self.usernameTF.rx:将textfiled用Reactive封装: .text:监 ...
- Redis 之服务器集群配置
常见的集群架构如图: redis操作过程中数据同步的函数调用关系: 集群搭建: 1.修改3个redis.config 文件的: 2.启动2个redis服务器 当杀掉redis主进程Master时,由于 ...
- 在centos安装 sql server
主要参考官方文档https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-setup-red-hat
- nodejs 文件操作模块 fs
const fs=require("fs"); //文件操作 //创建目录 ./ 代表当前目录 ../ 代表上级目录fs.mkdir('./test',function(err){ ...
- 模态框(layer)
推荐一个好看的模态框(layer) 地址:http://layer.layui.com/ 相应列子及配置 全部来自于官网,可直接访问官网学习了解. //信息框-例1 layer.alert('见 ...
- php第二十节课
JSON弹窗 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...
- qwb与小数
qwb与小数 Time Limit: 1 Sec Memory Limit: 128 MB Description qwb遇到了一个问题:将分数a/b化为小数后,小数点后第n位的数字是多少? 做了那 ...