给定 SQL 中的多个约束,如何找到具有相同值的连续行数?
How to find the consecutive number of rows with the same value, given multiple constraints in SQL?
我正在使用 SQL Server 2012,我的数据如下所示:
ActivityID ActivityCode Name_of_User ActivityCode_PrevActivity isLastActivityByUserSame
1020 B1 ABE A2 0
1021 A2 BOB A1 0
1022 A2 BOB A2 1
1023 B1 ABE B1 1
1024 B1 ABE B1 1
1025 B2 ABE B1 0
1026 B2 CARL A3 0
1027 A1 CARL B2 0
1028 B8 BOB A2 0
1029 A1 CARL A1 1
1030 B2 ABE B2 1
1031 B4 ABE B2 0
1032 B8 BOB B8 1
1033 A3 BOB B8 0
1034 B4 CARL A1 0
就简单概括一下字段代表什么:
- ActivityID: table
的PK
- Activity代码: 用户activity的类型
- Name_of_User:用户名
- ActivityCode_PrevActivity: Activity 用户最后 activity 的代码
- isLastActivityByUserSame: 表示用户上次activity代码是否与当前activity相同的二进制值代码
除此之外,还有一些其他列可能对手头的任务没有帮助。
我想生成一个 运行 计数,每行具有相同值的连续行数 Name_of_User。为了让您了解它的外观,这是预期的结果(注意新的 ConsecActivityCount 列):
ActivityID ActivityCode Name_of_User ActivityCode_PrevActivity isLastActivityByUserSame ConsecActivityCount
1020 B1 ABE A2 0 0
1023 B1 ABE B1 1 1
1024 B1 ABE B1 1 2
1025 B2 ABE B1 0 0
1030 B2 ABE B2 1 1
1031 B4 ABE B2 0 0
1021 A2 BOB A1 0 0
1022 A2 BOB A2 1 1
1028 B8 BOB A2 0 0
1032 B8 BOB B8 1 1
1033 A3 BOB B8 0 0
1026 B2 CARL A3 0 0
1027 A1 CARL B2 0 0
1029 A1 CARL A1 1 1
1034 B4 CARL A1 0 0
我尝试使用类似的东西:
Select *,
DENSE_RANK() OVER (PARTITION BY Name_of_User ORDER BY ActivityID) as ConsecReports
FROM ActivityTable
但是,我运气不好。
计算此类列的最佳方法是什么?
提前致谢!
我想我找到了一个很好的解决方案。我首先为它们出现的序列创建 ID,然后计算连续阳性的数量 'isLastActivityByUserSame'。
解决方法如下:
SELECT at.*,
(CASE WHEN at.isLastActivityByUserSame != 0
THEN SUM(CASE WHEN at.isLastActivityByUserSame != 0 THEN at.isLastActivityByUserSame END) OVER (PARTITION BY Name_of_User, LastSequenceID ORDER BY ActivityID)
ELSE 0 END) AS ConsecActivityCount
FROM (select at.*,
SUM(CASE WHEN isLastActivityByUserSame != 0 THEN 0 ELSE 1 END) OVER (ORDER BY ActivityID) AS LastSequenceID
FROM ActivityTable at
) at ORDER BY Name_of_User, ActivityID;
使用下面的查询。它会起作用。
SELECT *
,ROW_NUMBER()OVER(PARTITION BY Name_of_User, ActivityCode ORDER BY ActivityID)-1 AS ConsecActivityCount
FROM ActivityTable
我正在使用 SQL Server 2012,我的数据如下所示:
ActivityID ActivityCode Name_of_User ActivityCode_PrevActivity isLastActivityByUserSame
1020 B1 ABE A2 0
1021 A2 BOB A1 0
1022 A2 BOB A2 1
1023 B1 ABE B1 1
1024 B1 ABE B1 1
1025 B2 ABE B1 0
1026 B2 CARL A3 0
1027 A1 CARL B2 0
1028 B8 BOB A2 0
1029 A1 CARL A1 1
1030 B2 ABE B2 1
1031 B4 ABE B2 0
1032 B8 BOB B8 1
1033 A3 BOB B8 0
1034 B4 CARL A1 0
就简单概括一下字段代表什么:
- ActivityID: table 的PK
- Activity代码: 用户activity的类型
- Name_of_User:用户名
- ActivityCode_PrevActivity: Activity 用户最后 activity 的代码
- isLastActivityByUserSame: 表示用户上次activity代码是否与当前activity相同的二进制值代码
除此之外,还有一些其他列可能对手头的任务没有帮助。
我想生成一个 运行 计数,每行具有相同值的连续行数 Name_of_User。为了让您了解它的外观,这是预期的结果(注意新的 ConsecActivityCount 列):
ActivityID ActivityCode Name_of_User ActivityCode_PrevActivity isLastActivityByUserSame ConsecActivityCount
1020 B1 ABE A2 0 0
1023 B1 ABE B1 1 1
1024 B1 ABE B1 1 2
1025 B2 ABE B1 0 0
1030 B2 ABE B2 1 1
1031 B4 ABE B2 0 0
1021 A2 BOB A1 0 0
1022 A2 BOB A2 1 1
1028 B8 BOB A2 0 0
1032 B8 BOB B8 1 1
1033 A3 BOB B8 0 0
1026 B2 CARL A3 0 0
1027 A1 CARL B2 0 0
1029 A1 CARL A1 1 1
1034 B4 CARL A1 0 0
我尝试使用类似的东西:
Select *,
DENSE_RANK() OVER (PARTITION BY Name_of_User ORDER BY ActivityID) as ConsecReports
FROM ActivityTable
但是,我运气不好。
计算此类列的最佳方法是什么?
提前致谢!
我想我找到了一个很好的解决方案。我首先为它们出现的序列创建 ID,然后计算连续阳性的数量 'isLastActivityByUserSame'。
解决方法如下:
SELECT at.*,
(CASE WHEN at.isLastActivityByUserSame != 0
THEN SUM(CASE WHEN at.isLastActivityByUserSame != 0 THEN at.isLastActivityByUserSame END) OVER (PARTITION BY Name_of_User, LastSequenceID ORDER BY ActivityID)
ELSE 0 END) AS ConsecActivityCount
FROM (select at.*,
SUM(CASE WHEN isLastActivityByUserSame != 0 THEN 0 ELSE 1 END) OVER (ORDER BY ActivityID) AS LastSequenceID
FROM ActivityTable at
) at ORDER BY Name_of_User, ActivityID;
使用下面的查询。它会起作用。
SELECT *
,ROW_NUMBER()OVER(PARTITION BY Name_of_User, ActivityCode ORDER BY ActivityID)-1 AS ConsecActivityCount
FROM ActivityTable