title: SQL Server - How would I rank products from the last month?
body: <p>I've already started to flesh things out a bit, but I'm having trouble adding in some sort of window function to get the intended result of having the data, products, called in my code ranked by sales in the last month. If any help could be given, I would greatly appreciate it! </p>
  
  <p>Here is what is being asked:</p>
  
  <blockquote>
    <p>The CEO would like to know the sales based on last month's sales. 
    Please provide her with a query to rank the products by the number of
    orders in the last month.  There should be NO SKIPPED NUMBERS.</p>
  </blockquote>
  
  <p>Here is what I've got so far:</p>
  
  <pre><code>SELECT 
      P.NAME AS 'Product Name',
      Isnull(Count(DISTINCT O.OrderID), 0) AS 'Orders Count',
      Sum(Isnull(o.ordertotal, 0)) AS 'Orders Total',
      Sum (Isnull(oi.orderitemquantity, 0)) AS 'Item Total'
   FROM 
      Product P
   INNER JOIN 
      OrderItem OI ON OI.ProductID = P.ProductID
   INNER JOIN 
      Orders O ON O.OrderID = OI.OrderID
  GROUP BY 
      P.Name
  </code></pre>
  
  <p>This does need to be in a stored procedure as well, so any help with that would be great too.</p>