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. wordpress 你所不知道的固定链接设置方法,设置适合自己的个性固定链接,适合SEO

    %year% 年份,四位数字,例如2004年  %monthnum% 一年的月份,例如05  %day% 一个月的日子,例如28  %hour% 一天中的小时,例如15  %minute% 小时,例如 ...

  2. PHP基础知识测试题及解析

      本试题共40道选择题,10道判断题,考试时间1个半小时 一:选择题(单项选择,每题2分): 1. LAMP具体结构不包含下面哪种(A ) A:Windows系统 B:Apache服务器 C:MyS ...

  3. 使用码云gitee.com托管代码

    1.新建项目 可以看到团队资源管理器是这样的,已经在本地有存储库,所有更改可以保存本地 2.在码云上新建项目 项目名称必填,其它项根据情况填写 3.复制项目地址关联到本地存储库 填写码云的项目地址,发 ...

  4. (转)Quartz任务调度(1)概念例析快速入门

    http://blog.csdn.net/qwe6112071/article/details/50991563 Quartz框架需求引入 在现实开发中,我们常常会遇到需要系统在特定时刻完成特定任务的 ...

  5. HDU_3308_线段树_区间合并

    LCIS Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. 查询数据表行数 然后循环查找表 添加数据到ITEMS

    ;i<tbBiao.Rows.Count;i++) { string TableName = (tbBiao.Rows[i]["Table"]).ToString(); tb ...

  7. 云计算时代,你为什么一定要学Linux?

    云计算早已不是什么稀奇的概念,它的火爆让Linux运维工程师这个职业越来越重要.在当今各类云平台提供的系统中,Linux系统几乎毫无争议的独占鳌头,市场份额进一步扩张. 这也让Linux运维工程师职位 ...

  8. c++ list双向链表管理对象

    #cat list.cc #include <cstdlib> #include <iostream> #include <stdio.h> using names ...

  9. 29. 误拼写时的fuzzy模糊搜索技术

    搜索的时候,可能输入的搜索文本会出现误拼写的情况,这时就需要es为我们进行智能纠错 比如有两个文档: doc1: hello world doc2: hello java     现在要搜索:hall ...

  10. 【[Offer收割]编程练习赛12 A】歌德巴赫猜想

    [题目链接]:http://hihocoder.com/problemset/problem/1493 [题意] [题解] 枚举P从2..n/2 如果P是质数且N-P也是质数; 则输出P和N-P就好; ...