Attendance我的数据库中有一个表。
Attendance
Date | Present ------------------------ 20/11/2013 | Y 21/11/2013 | Y 22/11/2013 | N 23/11/2013 | Y 24/11/2013 | Y 25/11/2013 | Y 26/11/2013 | Y 27/11/2013 | N 28/11/2013 | Y
我想计算一个值Y或的最连续出现N。
Y
N
例如在上表中Y发生 2、4和1次 。所以我想要 4 作为结果。如何在SQL Server中实现这一目标?
任何帮助将不胜感激。
试试这个:-
连续日期之间的差额将保持不变
Select max(Sequence) from ( select present ,count(*) as Sequence, min(date) as MinDt, max(date) as MaxDt from ( select t.Present,t.Date, dateadd(day, -(row_number() over (partition by present order by date)) ,date ) as grp from Table1 t ) t group by present, grp )a where Present ='Y'
SQL字段