title: Getting the % Difference of two columns in SQL Server
body: <p>I am selecting a count for a column based on a given month/year. I need to return the difference in a percentage of that count compared to last months.</p>
  
  <p>Here's what I have thus far. This returns the first half of what I need (the current month count).</p>
  
  <pre><code>select 
      o.productName, COUNT(*) as totalSales 
  from 
      order o
  inner join 
      product p on o.productID=p.productID
  where 
      datePart(month, dateTimeReceived) = 2
      and datePart(year, dateTimeReceived) = 2012         
  group by 
      o.productName
  order by 
      totalSales desc
  </code></pre>
  
  <p>So the output of this appears as follows</p>
  
  <pre><code>Widget 1  - 200 sold
  Widget 2  - 190 sold
  </code></pre>
  
  <p>Again, I would also like to include a 3rd column which captures % change from the previous month.</p>
  
  <p>Also, I am not getting a warm fuzzy with regards to how I am selecting with <code>datePart</code> so if you have a recommendation on how to approach that better I would appreciate it!</p>