窗口函数 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
随机推荐
- Android 控制硬加速 hardwareAccelerated
从Android3.0 (API level11)开始,Android的2D显示管道被被设计得更加支持硬加速了.硬加速使用GPU承担了所有在View的canvas上执行的绘制操作. 启用硬加速最简单的 ...
- Recyclerview点击事件,更新item的UI+更新Recyclerview外的控件
项目中用到了Recyclerview,在第一行代码中学到了一种相对来说简单的点击事件方法,可是这种点击事件是在adapter中写的,没有教怎么更新item的ui和更新Recyclerview之外的控件 ...
- 04--Spring知识汇总
1. @Autowried注解 Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量.方法及构造函数进行标注,完成自动装配的工作. 通过 @Autowired的使用来消除 se ...
- 使用Kettle增量抽取MongoDB数据实践
需求: 增量抽取MongoDB数据并加载到MSSQL 由于不能使用关系型数据库的自定义SQL, 所以主要遇到的问题有: 增量时间的查询和参数控制 ETL的批次信息和调用参数的写入 第一个问题的解决如下 ...
- cms中某些标题链接的单独写法
href="{$CATEGORYS[45][url]}" 链接写法, {$CATEGORYS[45][catname]} 标题写法 在show页面中 src="{$thu ...
- JS——轮播图简单版
1.小图标版本 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- C# Winform 最大化后 任务栏还显示解决
//最大化 this.WindowState = FormWindowState.Maximized; //窗体最大化时 非全屏 不会遮盖任务栏 //去掉标题栏 this.FormBorderStyl ...
- Python 之re正则表达式
- c#中通过事件实现按下回车跳转控件
//接受用户输入参数后回车事件 private void tb_KeyPress(object sender, KeyPressEventArgs e) { ) { SendKeys.Send(&qu ...
- 黑苹果开启retina,大分辨率的方法
首先,管理分辨率RDM的软件这里下载: http://pan.baidu.com/s/1bpjL07P 在终端输入: curl -o ~/enable-HiDPI.sh https://raw.git ...