title: Calculate year to date duplicate
body: <p>I am trying to calculate year to date </p>
  
  <p>I try this script, it returns the good values but it's too slow </p>
  
  <pre><code>select 
          T.Delivery_month,
      T.[Delivery_Year],
      Sales_Organization,
      SUM(QTY) as Month_Total,
  
      COALESCE(
          (
              select SUM(s2.QTY) 
              FROM stg.Fact_DC_AGG s2 
              where 
                  s2.Sales_Organization = T.Sales_Organization 
                  and  s2.[Delivery_Year]=T.[Delivery_Year]
                  AND s2.Delivery_month&lt;= T.Delivery_month
  
          ),0) as YTD_Total
  from  stg.Fact_DC_AGG T
  group by 
      T.Delivery_month,
      T.[Delivery_Year],
      Sales_Organization
  ORDER BY 
      Sales_Organization,T.[Delivery_Year],
  T.Delivery_month
  </code></pre>
  
  <p>I modified it in order to optimize it, but it returns wrongs values with duplicates:</p>
  
  <pre><code>select 
          T.Delivery_month,
      T.[Delivery_Year],
      Sales_Organization,
      SUM(QTY) as Month_Total,
  
      COALESCE(
          (
  
  
          ),0) as YTD_Total
  from  stg.Fact_DC_AGG T
  INNER JOIN stg.Fact_DC_AGG s2 
              ON  
                  s2.Sales_Organization = T.Sales_Organization 
                  and  s2.[Delivery_Year]=T.[Delivery_Year]
                  AND s2.Delivery_month&lt;= T.Delivery_month
  group by 
      T.Delivery_month,
      T.[Delivery_Year],
      Sales_Organization
  ORDER BY 
      Sales_Organization,T.[Delivery_Year],
  T.Delivery_month
  </code></pre>
  
  <p>How to optimize the query or to correct the second script ?</p>