title: Convert Select Query into CTE
body: <pre><code>SELECT
      COUNT(*) AS Previous_Live_Members, m.HomeBranch, m.LocationName    
  FROM
      AX.Mem  M 
  WHERE
      m.ActiveStart &lt; DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 13, 0)
      AND (m.ActiveEnd &gt; DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 13, 0) OR M.ActiveEnd IS NULL)
  GROUP BY
      m.HomeBranch, m.LocationName
  </code></pre>
  
  <p>I already converted but results are different. Might be some issue in my query. I want to convert simple query into CTE table.</p>
  
  <pre><code>WITH CTE_A AS 
  (
      SELECT
          SUM(CASE 
                 WHEN M.ActiveStart &lt; DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 13, 0)
                      AND M.ActiveEnd &gt; DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 13, 0) OR M.ActiveEnd IS NULL
                   THEN 1 
                   ELSE 0 
              END) AS Previous_No_of_Live_Member,
          M.HomeBranch,
          M.LocationName
      FROM
          AX.Mem M 
      GROUP BY 
          M.HomeBranch, M.LocationName 
  )
  SELECT
      Previous_No_of_Live_Member,
      HomeBranch,
      LocationName
  FROM  
      CTE_A
  </code></pre>