显示连续几天数量

SELECT
    credentialNo,
    max(days) lianxu_days,
    min(login_day) start_date,
    max(login_day) end_date
FROM
    (
        SELECT
            credentialNo,
            @cont_day := (
                CASE
                WHEN (
                    @last_uid = credentialNo
                    AND DATEDIFF(login_dt, @last_dt) = 1
                ) THEN
                    (@cont_day + 1)
                WHEN (
                    @last_uid = credentialNo
                    AND DATEDIFF(login_dt, @last_dt) < 1
                ) THEN
                    (@cont_day + 0)
                ELSE
                    1
                END
            ) AS days,
            (
                @cont_ix := (
                    @cont_ix +
                    IF (@cont_day = 1, 1, 0)
                )
            ) AS cont_ix,
            @last_uid := credentialNo,
            @last_dt := login_dt login_day
        FROM
            (
                SELECT
                    credentialNo,
                    DATE(insertTime) AS login_dt
                FROM
                    e_access_log 
                ORDER BY
                    credentialNo,
                    insertTime
            ) AS t,
            (
                SELECT
                    @last_uid := '',
                    @last_dt := '',
                    @cont_ix := 0,
                    @cont_day := 0
            ) AS t1
    ) AS t2
GROUP BY
    credentialNo,
    cont_ix
HAVING lianxu_days>1

原文:https://blog.csdn.net/nydia_lvhq/article/details/49926557

你可能感兴趣的:(显示连续几天数量)