title: SQL Sum Column Between Two Dates
body: <p>I have problem to sum price column between two dates in SQL Server.</p>
  
  <p>I have query like this :</p>
  
  <pre><code>SELECT
      OSLP.SlpName as Salesman,
      CAST(OINV.DocTotal as float) as Achiev,
      OINV.TaxDate
  FROM 
      OINV
  INNER JOIN 
      INV1 ON INV1.DocEntry = OINV.DocEntry
  INNER JOIN 
      OSLP ON OINV.SlpCode = OSLP.SlpCode
  INNER JOIN 
      OITM ON INV1.ItemCode = OITM.ItemCode
  INNER JOIN 
      OMRC ON OITM.FirmCode = OMRC.FirmCode
  INNER JOIN 
      OCRD ON OINV.CardCode = OCRD.CardCode
  WHERE
      OSLP.SlpName LIKE '01-2 Ika' 
      AND OINV.TaxDate BETWEEN '20160804' AND '20160806'
  GROUP BY
      OSLP.SlpName, OINV.DocTotal, OINV.TaxDate
  </code></pre>
  
  <p>The result of the query above is like this :</p>
  
  <p><a href="https://i.stack.imgur.com/f26c3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/f26c3.png" alt="enter image description here"></a></p>
  
  <p>I have also added SUM on "CAST(OINV.DocTotal as float) as Achiev" like this :</p>
  
  <pre><code>CAST(sum(OINV.DocTotal) as float) as Achiev
  </code></pre>
  
  <p>But, it returns wrong result : </p>
  
  <p><a href="https://i.stack.imgur.com/kiUqP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kiUqP.png" alt="enter image description here"></a></p>
  
  <p>The correct result of the Achiev column on the dates should be <strong><code>4906230</code></strong></p>
  
  <p>Many thanks for the help!</p>
  
  <p><strong>UPDATE (SOLVED)</strong>
  I have solved the problem. It just add distinct to the SUM query, because there is a duplicate of data in my query.</p>
  
  <p>Query :</p>
  
  <pre><code>SELECT
    OSLP.SlpName as Salesman,
    sum(CAST(INV1.Quantity as float)) as Qty,
    sum(DISTINCT CAST(OINV.DocTotal as float)) as Achiev
  FROM 
    OINV
  INNER JOIN 
    INV1 ON INV1.DocEntry = OINV.DocEntry
  INNER JOIN 
    OSLP ON OINV.SlpCode = OSLP.SlpCode
  INNER JOIN 
    OITM ON INV1.ItemCode = OITM.ItemCode
  INNER JOIN 
    OMRC ON OITM.FirmCode = OMRC.FirmCode
  INNER JOIN 
    OCRD ON OINV.CardCode = OCRD.CardCode
  WHERE
    OSLP.SlpName LIKE '01-2 Ika' 
    AND OINV.TaxDate BETWEEN '20160804' AND '20160806'
  GROUP BY
    OSLP.SlpName
  </code></pre>
  
  <p>it returns :
  <a href="https://i.stack.imgur.com/yWx2I.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yWx2I.png" alt="enter image description here"></a></p>
  
  <p>Thanks to everyone who has helped me!</p>