SQL计数连续几天
发布时间:2021-01-21 20:49:17 所属栏目:MsSql教程 来源:网络整理
导读:这是SQL数据库数据: UserTableUserName | UserDate | UserCode-------------------------------------------user1 | 08-31-2014 | 232user1 | 09-01-2014 | 232user1 | 09-02-2014 | 0user1 | 09-03-2014 | 121user1 | 09-08-2014 | 122user1 | 09-09-2014 |
这是SQL数据库数据: UserTable UserName | UserDate | UserCode ------------------------------------------- user1 | 08-31-2014 | 232 user1 | 09-01-2014 | 232 user1 | 09-02-2014 | 0 user1 | 09-03-2014 | 121 user1 | 09-08-2014 | 122 user1 | 09-09-2014 | 0 user1 | 09-10-2014 | 144 user1 | 09-11-2014 | 166 user2 | 09-01-2014 | 177 user2 | 09-04-2014 | 188 user2 | 09-05-2014 | 199 user2 | 09-06-2014 | 0 user2 | 09-07-2014 | 155 如果[UserCode]不是零,则应仅计算连续天数(如果为结果). 我希望我的sql查询返回的是: UserName | StartDate | EndDate | Result ---------------------------------------------------------- user1 | 09-01-2014 | 09-03-2014 | 2 user1 | 09-08-2014 | 09-11-2014 | 3 user2 | 09-04-2014 | 09-07-2014 | 3 这只能使用SQL查询吗? 解决方法这是一个 Gaps and Islands问题.解决此问题的最简单方法是使用ROW_NUMBER()来识别序列中的间隙:SELECT UserName,UserDate,UserCode,GroupingSet = DATEADD(DAY,-ROW_NUMBER() OVER(PARTITION BY UserName ORDER BY UserDate),UserDate) FROM UserTable; 这给出了: UserName | UserDate | UserCode | GroupingSet ------------+---------------+------------+------------- user1 | 09-01-2014 | 1 | 08-31-2014 user1 | 09-02-2014 | 0 | 08-31-2014 user1 | 09-03-2014 | 1 | 08-31-2014 user1 | 09-08-2014 | 1 | 09-04-2014 user1 | 09-09-2014 | 0 | 09-04-2014 user1 | 09-10-2014 | 1 | 09-04-2014 user1 | 09-11-2014 | 1 | 09-04-2014 user2 | 09-01-2014 | 1 | 08-31-2014 user2 | 09-04-2014 | 1 | 09-02-2014 user2 | 09-05-2014 | 1 | 09-02-2014 user2 | 09-06-2014 | 0 | 09-02-2014 user2 | 09-07-2014 | 1 | 09-02-2014 如您所见,这为连续行的GroupingSet提供了一个常量值.然后,您可以按此列分组以获取所需的摘要: WITH CTE AS ( SELECT UserName,-ROW_NUMBER() OVER(PARTITION BY UserName ORDER BY UserDate),UserDate) FROM UserTable ) SELECT UserName,StartDate = MIN(UserDate),EndDate = MAX(UserDate),Result = COUNT(NULLIF(UserCode,0)) FROM CTE GROUP BY UserName,GroupingSet HAVING COUNT(NULLIF(UserCode,0)) > 1 ORDER BY UserName,StartDate; Example on SQL Fiddle (编辑:上饶站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |