窗口函数 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
随机推荐
- css3通过scale()实现放大功能、通过rotate()实现旋转功能
css3通过scale()实现放大功能.通过rotate()实现旋转功能,下面有个示例,大家可以参考下 通过scale()实现放大功能 通过rotate()实现旋转功能 而transition则可设置 ...
- Android Unable to add window -- token android.os.BinderProxy@3a067204 is not valid错误分析记录
打开APP时,出现闪退的情况,查看android studio报错信息,主要为: Unable to add window -- token android.os.BinderProxy@3a0672 ...
- Windows开发小问题集
ON_BN_KILLFOCUS无效,因为需要BS_NOTIFY
- CSS——行高
浏览器默认文字大小:16px 行高:是基线与基线之间的距离 行高=文字高度+上下边距 一行文字行高和父元素高度一致的时候,垂直居中显示. <!DOCTYPE html> <html& ...
- 在centos安装 sql server
主要参考官方文档https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-setup-red-hat
- C 利用strtok, feof 截取字符串
#cat /tmp/fff 10:hugetlb:/hello/06b11c9967cc0e106f5f4673246f671aa7388f623f58b250d9d9cb0f8c0f2b18 9:d ...
- java折半插入排序
代码如下: public class BinaryInsertSort { public static void binaryInsertSort(DataWrap [] data) { System ...
- Codeforces 939D - Love Rescue
传送门:http://codeforces.com/contest/939/problem/D 本题是一个数据结构问题——并查集(Disjoint Set). 给出两个长度相同,且仅由小写字母组成的字 ...
- Train Problem II HDU 1023 卡特兰数
Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want ...
- 洛谷 P3078 [USACO13MAR]扑克牌型Poker Hands
P3078 [USACO13MAR]扑克牌型Poker Hands 题目描述 Bessie and her friends are playing a unique version of poker ...