title: How to COUNT for 3 consecutive months or more from a Date column in SQL Server?
body: <p><img src="https://i.stack.imgur.com/OgTAC.png" alt="Click here for Small Sample Data for reference "></p>
  
  <p>I'm trying to have my query COUNT the number of user rows but have 3 + months of consecutive data (aka rows passed). I was wonder how can you determine from a date column if whether or not an Employee passed 3 consecutive months or more of data/rows in the table?</p>
  
  <p>I've tried using COUNT function and attempting to use temp tables but wanted to see if there was a better way to count each user by their departments who pass 3 consecutive months or more?</p>
  
  <p><img src="https://i.stack.imgur.com/y0j06.png" alt="Output Data"></p>
  
  <pre><code>DECLARE @TotalUserCount AS INT
  SELECT @TotalUserCount = COUNT(DISTINCT [User_ID])
  FROM [dbo].[Employee_Monthly_Raw]
  
  SELECT DISTINCT a.Department AS 'Dep', @TotalUserCount AS 'Total_User_Count'
  FROM
      (SELECT DISTINCT 
      RTRIM(LTRIM([Department])) AS 'Dep',
      [User_ID] AS 'User_ID',
      COUNT(ALL @TotalUserCount) AS 'Total_User_Count',
      COUNT(DISTINCT [Date]) AS 'Baseline',
      FROM [dbo].[Employee_Monthly_Raw]
      WHERE 
      [User_ID] IS NOT NULL
  
      GROUP BY [Dep],[User_ID]
          HAVING COUNT(DISTINCT [Date]) &gt;= '6') AS a
  
  GROUP BY [Department]
  ORDER BY [Department]
  </code></pre>
  
  <p>Expected results should pass a count of EEs (Group by their Department) in which they pass 3 months or more of data from the 'Date' column that is entered in the first of the month format (i.e. '01/01/2019')</p>