title: Query help on CTE calculation for average - SQL
body: <h2>I can't seem to calculate the average quantity sold per order per day.</h2>
  <p>The join seems to be replacing my values. I'm unsure how to calculate between different entities and maintain the value. My guess is it has something to do with nulls and is matching it without them?
  --2) Using Sales.SalesOrderHeader and Sales.SalesOrderDetail, determine the
  --average quantity sold per order per day. Use a CTE to determine the answer.</p>
  <pre><code>WITH cte AS 
  (
  SELECT 
  SUM(d.OrderQty) AS OrderQtyTotal, 
  COUNT(d.SalesOrderID) AS IndSales,  
  COUNT(h.OrderDate) AS Days
  
  FROM Sales.SalesOrderDetail AS d
      
  RIGHT JOIN Sales.SalesOrderHeader AS h
  ON d.SalesOrderDetailID = h.SalesOrderID
  )
  
  SELECT ((OrderQtyTotal/IndSales)/Days)
  FROM cte
  </code></pre>