2016-06-20 29 views
0

比方說,我有這個表:大多數號碼的連續登錄天

date  | has_login 
------------------------ 
2015-01-01 | 1 
2015-01-02 | 1 
2015-01-03 | 0 
2015-01-04 | 1 
2015-01-05 | 1 
2015-01-06 | 1 
2015-01-07 | 0 
2015-01-08 | 1 

我如何獲得最大數量的連續登錄的? 在這種情況下,它應該是

+9

檢查this..it可能help..it詳細http://stackoverflow.com/questions/21472483/同一個問題的談判如何對數最1連續出現對的一值-IN-A-列在-SQL服務器 –

回答

0

嘗試

select max(cons) as max_consecutives 
from 
(
    select @cons := case when has_login = 1 
         then @cons + 1 
         else 1 
        end as cons 
    from your_table 
    cross join (select @cons := 0) c 
    order by date 
) tmp