I think it works. With MSSQL, you do not need to duplicate views.
SELECT l1.user,l1.thedate, COUNT(*) CNT FROM
(
SELECT st.user,st.thedate,COUNT(*) locs FROM sotest st
GROUP BY st.user,st.thedate
HAVING( COUNT(DISTINCT st.location) > 1)
) l1
JOIN
(
SELECT st.user,st.thedate,COUNT(*) locs FROM sotest st
GROUP BY st.user,st.thedate
HAVING( COUNT(DISTINCT st.location) > 1)
) l2 ON l2.user = l1.user and DATEDIFF(l2.thedate , l1.thedate) = 1
JOIN
(
SELECT st.user,st.thedate,COUNT(*) locs FROM sotest st
GROUP BY st.user,st.thedate
HAVING( COUNT(DISTINCT st.location) > 1)
) l3 ON l3.user = l2.user and DATEDIFF(l3.thedate , l2.thedate) = 1
GROUP BY l1.user,l1.thedate
Each of the views defines the days when the user has 2 or more locations.
source
share