窗口函数笔记

over()

1.distinct 2.group by

partition by 子句
over(partition by(condition) order by condition)

window 子句

over(partition by(condition)order by condiiton rows between unbounded preceding and current row)
over(partition by(condition) order by condition rows between 数字 preceding and current row)
over(partition by(condition) order by condition rows between 数字 preceding and 数字 following)
over(partiton by(condition) order by condition rows  between current row and unbounded following )

序列函数
ntile(切片)
ntile不支持row between 
ntile(需要切成几片的数字) over(partition by(condition)order by(condition))

row_number 对目标进行排序,即使有相同的数字,也会有前后的数字大小顺序,并且数字是连续  的。
rank()     对目标进行排序,当出现相同数字时,会以相同数字排序,但是会留下空位。
即加入第四位和第五位数字大小相同,rank()排序标记相同顺序数字大小,这样会跳过排序5号数字
dense_rank() 与rank()大体相同,区别在于它不会有空位,数字也是连续的,只是相同的数字会打上
相同的排序顺序


lag(condition,数字n) 每一行查询结果返回上n行condition
lag(condition,数字n,string) 每一行返回上n行数据,为空的行则默认string
lead(conditon,数字n,string) 用法与lag相似,返回下n行数据,为空默认string

first_value 截至到当前行的第一个值
first_value(condition)over(partition by(condition) order by(condition))
last_value 截至当前行的最后一个值
last_value(condition)over(partiton by(condition) order by(condition))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQL Server窗口函数是一种用于进行分组或分区计算的函数。它可以与聚合函数或排序函数结合使用。窗口函数在SQL Server中被用于实现OLAP(在线分析处理)功能,并且可以对数据库中的数据进行实时分析处理。窗口函数的分类包括能够作为窗口函数的聚合函数(如SUM、AVG、COUNT、MAX、MIN)以及专用窗口函数(如RANK、DENSE_RANK、ROW_NUMBER)。这些函数可以根据指定的分组或分区对数据进行计算,并返回结果。因此,SQL Server窗口函数在数据分析和报表制作中起着重要的作用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [SQL Sever 学习笔记十一——窗口函数、GROUPING运算符](https://blog.csdn.net/weixin_45666566/article/details/106407762)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [SQL SERVER中的开窗函数](https://blog.csdn.net/sinat_25172701/article/details/48084511)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值