title: Collecting multiple counts in one table
body: <p>I have a job log table. I would like to know how many jobs were running per minute for a 2 hour time period on each server.</p>
  
  <p>I know I can do this in PHP or PERL. Is there a pure SQL way to accomplish this task?</p>
  
  <p>I believe I need to do a while loop starting with a date/time variable and a integer variable for max minutes. Can a while loop be done in a sql statement or do I need a stored procedure?</p>
  
  <p>This is the Stored Proc I have written. I know it does not store any data yet.</p>
  
  <p>MySQL complains with a no DB selected when I try to run apply it:</p>
  
  <pre><code>DELIMITER //
  
  CREATE PROCEDURE `StatsByMinute`(IN StartDate datetime, 
                                   IN NumMin integer, 
                                   IN WSServer varchar(16))
  BEGIN
     DECLARE c INT;
     SET c = -1;
     WHILE c &lt;= NumMin DO 
        SELECT COUNT(*) 
        FROM tws.SymphonyJobs 
        WHERE JobStarted   &lt;= TIMESTAMPADD(MINUTE,1+c,StartDate)
          AND JobCompleted &gt;= TIMESTAMPADD(MINUTE,2+c,StartDate)
          AND JobWS= WSServer;
        SET c=c+1; 
     END WHILE;
  END
  </code></pre>
  
  <p>Any help would be appreciated.</p>
  
  <p>Sample data:</p>
  
  <pre><code>StreamWS    JobStarted  JobCompleted
  Server1 4/12/2014 21:31 4/12/2014 21:53
  Server1 4/12/2014 21:31 4/12/2014 21:53
  Server1 4/12/2014 21:31 4/12/2014 21:53
  Server1 4/12/2014 21:31 4/12/2014 21:53
  Server1 4/12/2014 21:31 4/12/2014 21:53
  Server1 4/12/2014 21:31 4/12/2014 21:53
  Server1 4/12/2014 21:31 4/12/2014 21:53
  Server1 4/12/2014 21:31 4/12/2014 21:53
  Server2 4/13/2014 0:01  4/13/2014 0:03
  Server2 4/13/2014 0:01  4/13/2014 0:03
  Server2 4/13/2014 0:01  4/13/2014 0:03
  Server2 4/13/2014 0:01  4/13/2014 0:03
  Server2 4/13/2014 0:01  4/13/2014 3:28
  Server2 4/13/2014 0:02  4/13/2014 0:03
  Server2 4/13/2014 0:02  4/13/2014 0:03
  Server2 4/13/2014 0:02  4/13/2014 0:03
  Server2 4/13/2014 0:02  4/13/2014 0:03
  Server2 4/13/2014 0:02  4/13/2014 0:03
  Server2 4/13/2014 0:02  4/13/2014 0:03
  Server2 4/13/2014 0:02  4/13/2014 0:03
  </code></pre>