title: SQL Server Query group by clause
body: <p>I have written Query. Below is that query</p>
  
  <pre><code>SELECT
    COUNT(amount) AS NR_Sales,
    DATEPART(YEAR, crn_dt) AS years
  FROM c_Payment_service_fee
  WHERE CFK_F_key IN (SELECT
    FK_2290F_key
  FROM C_Submissions
  WHERE FK_C_UP_key IN (SELECT
    PK_C_UP_key
  FROM C_User_Profile
  WHERE DATEPART(WEEK, crn_dt) BETWEEN 1 AND (DATEPART(WEEK, @date)))
  AND DATEPART(WEEK, crn_dt) BETWEEN 1 AND (DATEPART(WEEK, @date)))
  AND DATEPART(WEEK, crn_dt) BETWEEN 1 AND (DATEPART(WEEK, @date))
  AND amount &gt; 0
  GROUP BY DATEPART(YEAR, crn_dt)
  </code></pre>
  
  <p>And one more </p>
  
  <pre><code>SELECT
    amount AS NR_Sales,
    DATEPART(YEAR, crn_dt) AS years
  FROM c_Payment_service_fee
  WHERE CFK_F_key IN (SELECT
    FK_2290F_key
  FROM C_Submissions
  WHERE FK_C_UP_key IN (SELECT
    PK_C_UP_key
  FROM C_User_Profile
  WHERE DATEPART(WEEK, crn_dt) BETWEEN 1 AND (DATEPART(WEEK, @date))
  AND DATEPART(YEAR, crn_dt) = 2019)
  AND DATEPART(YEAR, crn_dt) = 2019
  AND DATEPART(WEEK, crn_dt) BETWEEN 1 AND (DATEPART(WEEK, @date)))
  AND DATEPART(WEEK, crn_dt) BETWEEN 1 AND (DATEPART(WEEK, @date))
  AND amount &gt; 0
  AND DATEPART(YEAR, crn_dt) = 2019
  </code></pre>
  
  <p>Both are working fine no error. But my problem is if i run first query 2019 count will be 303 and if run second query 269 data is displaying of 2019 year.</p>
  
  <p>Where I making mistaking i am not able find the problem please help me with that.</p>