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)的更多相关文章

  1. 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 ...

  2. SQL-49 针对库中的所有表生成select count(*)对应的SQL语句

    题目描述 针对库中的所有表生成select count(*)对应的SQL语句CREATE TABLE `employees` (`emp_no` int(11) NOT NULL,`birth_dat ...

  3. oracle(sql)基础篇系列(一)——基础select语句、常用sql函数、组函数、分组函数

        花点时间整理下sql基础,温故而知新.文章的demo来自oracle自带的dept,emp,salgrade三张表.解锁scott用户,使用scott用户登录就可以看到自带的表. #使用ora ...

  4. Ora中select某时间段记录sql语句

    要查找某时间段的记录,例如查找2013-11-1到2013-11-30的记录. ' group by user_name, user_id 注意:SQL语句中含有sum累加函数,末尾要加group b ...

  5. MySQL查询order by相减select相减的Sql语句

    先看一张表 create_time是订单创建时间,pay_time是支付时间 现在我想按照订单完成耗时的时间进行排序,并且取出来的数据中直接算好了差值,怎么用Sql呢,请看 select id,tid ...

  6. oracle(sql)基础篇系列(一)——基础select语句、常用sql函数、组函数、分组函数

    花点时间整理下sql基础,温故而知新.文章的demo来自oracle自带的dept,emp,salgrade三张表.解锁scott用户,使用scott用户登录就可以看到自带的表. #使用oracle用 ...

  7. 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 ...

  8. select into outfile的sql语句

             SELECT INTO…OUTFILE语句把表数据导出到一个文本文件中,并用LOAD DATA …INFILE语句恢复数据.但是这种方法只能导出或导入数据的内容,不包括表的结构,如果 ...

  9. Oracle 11g 、 Oracle 11g select 、 PLSQL 、 Sql Server迁移助手(SSMA)6.0/7.1 网盘下载地址

    - - - - - - - - 链接: https://pan.baidu.com/s/1q-uwAfeLOPxzBBx6V1pYLg 提取码: hei9

随机推荐

  1. Linux+Apache+PHP+MySQL服务器环境配置(CentOS篇)

    1.配置php.ini vi /etc/php.ini 2.配置apache 先给需要配置的文件做个备份 cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/h ...

  2. Canvas——基本入门

    基本概念 1.canvas 是 HTML5 提供的一个用于展示绘图效果的标签. canvas 原意画布, 帆布. 在 HTML 页面中用于展示绘图效果. 最早 canvas 是苹果提出的一个方案, 今 ...

  3. html5——:hover事件触发自己的:afert伪元素事件

    :hover事件触发自己的:afert伪元素事件中间是没有空格的

  4. Hibernate自动事务揪出的编码不规范

    最近重构的项目(Java初学中),Service层一个获取通知记录报错: org.springframework.dao.InvalidDataAccessResourceUsageException ...

  5. css图片高清适配

    同一张图片,在普通屏显示正常,但高清屏出现模糊.原因是原来一个像素的点分成的四个像素的点进行了显示. 解决方案:在高清屏中把图片变成二倍图,前提是二倍的高清图已经存在. .icon{ backgrou ...

  6. java_第一个servlet小程序

    xml中注册: <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>s ...

  7. C# 计算百分比

    //计算比率 decimal A =(decimal) 200.20; decimal B = (decimal)1000.20; decimal t = decimal.Parse((A/B).To ...

  8. css流光效果

    css流光效果1: <!DOCTYPE html> <html> <head> <title>ww</title> </head> ...

  9. Python os模块和time模块 day4

    一.os模块 print(os.listdir(r'/Users/smh/Desktop/整理'))#os.listdir() 列出某个目录下面的文件夹/文件 print(os.path.isfile ...

  10. javaWEB中web.xml配置文件相关

    1.常用节点的执行顺序: context-param -> listenter -> filter -> servlet -> interceptor,其中有多个filter的 ...