本文主要介绍了SQLServerRANK()排名函数的使用,具体如下:--例子表数据SELECT*FROMtest;--统计分数SELECTname,SUM(achievement)achievementFROMtestGROUPBYname;--按统计分数做排行SELECTRANK()OVER(ORDERBYSUM(achievement)desc)排行,name,SUM(achievement)achievementFROMtestGROUPBYname;求助问答存储过程使用:USE[DB]GO/******Object:StoredProcedure[dbo].[sp_TodayJoinUser]ScriptDate:2021/1/2614:45:24******/SETANSI_NULLSONGOSETQUOTED_IDENTIFIERONGO--=============================================--Author:_Hey_Jude--Createdate:2021-01-26--Description:获取今日发表帮助/回复的新用户--
本文主要介绍了SQLServer RANK() 排名函数的使用,具体如下:
-- 例子表数据SELECT * FROM test;-- 统计分数SELECT name,SUM(achievement) achievement FROM test GROUP BY name; -- 按统计分数做排行SELECT RANK() OVER( ORDER BY SUM(achievement) desc) 排行,name,SUM(achievement) achievement FROM test GROUP BY name;
求助问答存储过程使用:
USE [DB]GO /****** Object: StoredProcedure [dbo].[sp_TodayJoinUser] Script Date: 2021/1/26 14:45:24 ******/SET ANSI_NULLS ONGO SET QUOTED_IDENTIFIER ONGO -- =============================================-- Author: _Hey_Jude-- Create date: 2021-01-26-- Description: 获取今日发表帮助/回复的新用户-- =============================================CREATE PROCEDURE [dbo].[sp_TodayJoinUser] @tableLevel int, @date varchar(30)ASDeclare @Sql nvarchar(max) declare @minTabId intdeclare @maxTabId intdeclare @maxf_id intdeclare @helpTableName nvarchar(max)declare @tableCount int BEGIN --最小f_id所在表 set @minTabId=0 set @tableCount=@minTabId --最大f_id所在表 set @maxf_id=(select MAX(F_ID) from [Table] where F_IsDelete=0) set @maxTabId=@maxf_id/@tablelevel set @helpTableName='SELECT UserID, Max([F_DateTime]) AS dt FROM [Table] GROUP BY UserID' while @tableCount<=@maxTabId begin print @tableCount set @helpTableName += ' UNION SELECT UserID, Max([DateTime]) as dt FROM SubTable'+cast(@tableCount as nvarchar(10))+' GROUP BY UserID ' set @tableCount=@tableCount+1 end set @Sql='SELECT [nikename] FROM ( SELECT UserID, RANK() OVER(PARTITION BY UserID ORDER BY dt) AS Num,dt FROM ( '+@helpTableName+' ) AS T ) AS NewT LEFT JOIN [UserTable] A WITH(NOLOCK) ON NewT.UserID = A.UserId WHERE Num = 1 AND dt > '''+@date+'''' Exec sp_executesql @SqlEND GO
partition的意思是对数据进行分区,sql语句如下
SELECT* FROM ( SELECT ROW_NUMBER() over(partition by [姓名] order by [打卡时间] desc) as rowNum, [姓名], [打卡时间] FROM [dbo].[打卡记录表] ) tempWHERE temp.rowNum = 1
通过 partition by [姓名] order by [打卡时间] desc,这句就可以做到,让数据按照姓名分组,并且在每组内部按照时间进行排序
到此这篇关于SQLServer RANK() 排名函数的使用的文章就介绍到这了,更多相关SQLServer RANK()内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
标签: nbsp SQLServer RANK 排名 函数 使用
声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!