且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何计算 SQL Server 中列中值的最连续出现次数

更新时间:2022-12-09 12:16:21

试试这个:-

连续日期之间的差异将保持不变

The difference between the consecutive date will remain constant

   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 FIDDLE