title: DATEDIFF showing purchases from the last 30, 60, and 90 days
body: <p>Your boss loved the pivot query you wrote in the first project. He showed the accountant, and she said she wants to see a similar query, but with sales in the last 30, 60, and 90 days.</p>
  
  <p>MySQL has a function that can be used to compare two dates - DATEDIFF:
  SELECT DATEDIFF(now(), DateOfPurchase) FROM Purchases
  MySQL subtracts the DateOfPurchase from "now" and returns an integer representing the number of days between those two dates.</p>
  
  <p>You can use this function to write your accountant's requested pivot query. The results might look similar to the following:</p>
  
  <pre><code> Album         D30  D60  D90
  
   OK Computer    2    5    3 
   Sea Change     8    6    2 
  </code></pre>
  
  <p>Note that each time period has separate data. D60 data should be for purchases from 31 to 60 days ago, not everything purchased in the last 60 days. Similarly, D90 data should be for purchases from 61 to 90 days ago, not everything purchased in the last 90 days.</p>
  
  <p>Do not create a separate table or a view.</p>
  
  <p>Save your query as dba1lesson10project2.sql and then hand in the project.</p>
  
  <p>I have entered Purchase data into my Purchases table that are 30 60 and 90 days apart.</p>
  
  <p>My table looks like</p>
  
  <pre><code> Customer ID     DateOfPurchase     SongID
  
       1              2007-03-31        3
       3              2007-06-30        4
       4              2007-09-30        4
       5              2007-12-31        5
       2              2013-08-21        2
       4              2013-07-22        5
       5              2013-06-22        1
  </code></pre>
  
  <p>So, I'm only going to have 1 purchase per 30 day period to show.
  My previous project was to show purchases per quarter, which is why the 2007 data is in there.
  I've been speaking with my instructor regarding this, and he said my code for this would be similar to my code for my last project.</p>
  
  <p>I have tried</p>
  
  <pre><code> SELECT DATEDIFF(now(), DateOfPurchase),
   SUM(CASE WHEN DateOfPurchase &lt;= 30 THEN 1 ELSE 0 END) AS 'D30',
   SUM(CASE WHEN DateOfPurchase &gt;= 31 AND DateOfPurchase = 60 THEN 1 ELSE 0 END) AS 'D60',
   SUM(CASE WHEN DateOfPurchase &gt;= 61 AND DateOfPurchase = 90 THEN 1 ELSE 0 END) AS 'D90'
   FROM Purchases;
  </code></pre>
  
  <p>Which returns:</p>
  
  <pre><code> DATEDIFF(now(), DateOfPurchase   D30   D60   D90
  
                2365                 0     0     0
  </code></pre>
  
  <p>I have tried a couple different variants of this code, which pretty much return the same result. Other than the number in the first column being the same.</p>
  
  <p>The code to my last project is below, if that helps at all.</p>
  
  <pre><code> SELECT A.Title,
   SUM(CASE WHEN QUARTER(DateOfPurchase) = 1 THEN 1 ELSE 0 END) AS 'Q1',
   SUM(CASE WHEN QUARTER(DateOfPurchase) = 2 THEN 1 ELSE 0 END) AS 'Q2',
   SUM(CASE WHEN QUARTER(DateOfPurchase) = 3 THEN 1 ELSE 0 END) AS 'Q3',
   SUM(CASE WHEN QUARTER(DateOfPurchase) = 4 THEN 1 ELSE 0 END) AS 'Q4'
   FROM Albums AS A
   INNER JOIN Songs AS S
   ON A.AlbumID = S.AlbumID
   INNER JOIN Purchases as P
   ON S.SongID = P.SongID
   GROUP BY A.Title;
  </code></pre>
  
  <p>I'm also aware I will have to do the table joins for this project also. I'm just trying to get the correct output for D30 D60 and D90 before I do that.</p>