title: MySQL data - Count number of phone calls at the same time
body: <p>I have a MySQL table with phone calls. Every row means one phone call.
  Columns are:</p>
  
  <pre><code>start_time
  start_date
  duration
  </code></pre>
  
  <p>I need to get a maximum phone calls called at the same time. It's because of telephone exchange dimensioning.</p>
  
  <p>My solution is to create two timestamp columns <code>timestamp_start</code> and <code>timestamp_end</code>. Then I run a loop <em>second by second, day by day</em> and ask MySQL something like:</p>
  
  <pre><code>SELECT Count(*) FROM tbl WHERE start_date IN (thisday, secondday) AND "this_second_checking" BETWEEN timestamp_start AND timestamp_end;
  </code></pre>
  
  <p>It's quite slow.
  Is there a better solution? Thank you!</p>
  
  <p>EDIT - I use this solution and it gives me proper results. There is used SQL layer dibi - <a href="http://dibiphp.com/cs/quick-start" rel="nofollow">http://dibiphp.com/cs/quick-start</a> .</p>
  
  <pre><code>$starts = dibi::query("SELECT ts_start, ts_end FROM " . $tblname . " GROUP BY ts_start");
  if(count($starts) &gt; 0):
    foreach ($starts as $row) {
      if(isset($result)) unset($result);
      $result = dibi::query('SELECT Count(*) FROM ' . $tblname . ' WHERE "'.$row-&gt;ts_start.'" BETWEEN ts_start AND ts_end');
      $num = $result-&gt;fetchSingle();
      if($total_max &lt; $num):
        $total_max = $num;
      endif;    
    }  
  endif;
  echo "Total MAX: " . $total_max;
  </code></pre>