为了找到这个答案,我做了一些google搜索,甚至遇到了针对SQL Server的OVER()函数和一篇关于如何模拟的文章……但它高于我的工资等级:P
我会简单地说,我有一张学生桌和一个行动专栏。我想创建一个查询,它将统计他们举手、不举手或不上课的连续次数。没有周末课程,所以它必须计算实际记录,而不仅仅是今天的FirstFoundDate。如果学生A连续3周举手(计数为15=5天X 3周),但随后没有出现在课堂上,则计数为0。
有什么想法吗?
谢谢,

最佳答案

我一直在想这个,让我们重新开始。。。
怎么样

select *
from student_action maindata
where not exist (select * from student_action action_count
                 where maindata.student_id = action_count.student_id
                   and maindata.action_timestamp < action_count.action_timestamp
                   and maindata.action <> action_count.action)

这将为我们提供表中的所有连续操作,每连续出现一行。然后旁边的人会数数。
select count(*), maindata.student_id, maindata.action
from student_action maindata
where not exist (select * from student_action action_count
                 where maindata.student_id = action_count.student_id
                   and maindata.action_timestamp < action_count.action_timestamp
                   and maindata.action <> action_count.action)
group by maindata.student_id, maindata.action

07-26 00:27