Grouping by date range in a column I have a table with data as:

ID    Action    Date
1     Action1   2014-01-20
2     Action1   2014-01-22
3     Action1   2014-01-28
4     Action2   2014-01-20
5     Action2   2014-01-22
6     Action2   2014-01-28


I need to group by Date, grouping with 3 days of difference:

Action       Count
Action1      2
Action2      2


If a record is grouped with a previous record, it won't be in another group:

ID    Action    Date
1     Action1   2014-01-20
2     Action1   2014-01-23
3     Action1   2014-01-24

Action       Count
Action1      2


My solution:

SELECT T1.Action, DATEDIFF(DAY,T1.Date,T2.Date) as Count
FROM Table T1
JOIN Table T2 ON T1.Action=T2.Action
WHERE DATEDIFF(DAY,T1.Date,T2.Date) BETWEEN 1 AND 3
GROUP BY T1.Action,T1.Date,T2.Date


Is this a correct solution?
In SQL-Server 2012 version, looks straightforward with the LAG() function:

SELECT
    Action,
    Number = COUNT(*) 
FROM
    ( SELECT 
          Action, 
          Diff = DATEDIFF(day, LAG(Date) OVER (PARTITION BY Action ORDER BY Date), Date)  
      FROM
          a
    ) AS t
WHERE 
    Diff &gt; 3 OR Diff IS NULL
GROUP BY 
    Action ;


This should work, even in 2008 version:

SELECT
    Action,
    Number = COUNT(*) 
FROM
    a
WHERE 
    NOT EXISTS 
    ( SELECT *
      FROM a AS b
      WHERE b.Action = a.Action
        AND ( (b.Date &gt;= DATEADD(day, -3, a.Date) AND b.Date &lt; a.Date)
           OR (b.Date = a.Date AND b.ID &lt; a.ID)
            )
    ) 
GROUP BY 
    Action ;