Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL data - Count number of phone calls at the same time

Tags:

php

mysql

I have a MySQL table with phone calls. Every row means one phone call. Columns are:

start_time
start_date
duration

I need to get a maximum phone calls called at the same time. It's because of telephone exchange dimensioning.

My solution is to create two timestamp columns timestamp_start and timestamp_end. Then I run a loop second by second, day by day and ask MySQL something like:

SELECT Count(*) FROM tbl WHERE start_date IN (thisday, secondday) AND "this_second_checking" BETWEEN timestamp_start AND timestamp_end;

It's quite slow. Is there a better solution? Thank you!

EDIT - I use this solution and it gives me proper results. There is used SQL layer dibi - http://dibiphp.com/cs/quick-start .

$starts = dibi::query("SELECT ts_start, ts_end FROM " . $tblname . " GROUP BY ts_start");
if(count($starts) > 0):
  foreach ($starts as $row) {
    if(isset($result)) unset($result);
    $result = dibi::query('SELECT Count(*) FROM ' . $tblname . ' WHERE "'.$row->ts_start.'" BETWEEN ts_start AND ts_end');
    $num = $result->fetchSingle();
    if($total_max < $num):
      $total_max = $num;
    endif;    
  }  
endif;
echo "Total MAX: " . $total_max;
like image 471
Xdg Avatar asked Oct 25 '11 09:10

Xdg


People also ask

How do I count the number of records in a MySQL query?

The COUNT() function returns the number of records returned by a select query.

How do I count multiple values in SQL?

You can count multiple COUNT() for multiple conditions in a single query using GROUP BY. SELECT yourColumnName,COUNT(*) from yourTableName group by yourColumnName; To understand the above syntax, let us first create a table. The query to create a table is as follows.

How do I count number of employees in MySQL?

SELECT COUNT(*) AS "Number of employees" FROM employees WHERE salary > 75000; In this COUNT function example, we've aliased the COUNT(*) expression as "Number of employees". As a result, "Number of employees" will display as the field name when the result set is returned.

How do I count the number of columns in a table in MySQL?

mysql> SELECT COUNT(*) AS NUMBEROFCOLUMNS FROM INFORMATION_SCHEMA. COLUMNS -> WHERE table_schema = 'business' AND table_name = 'NumberOfColumns'; The output displays the number of columns.


1 Answers

Instead of running it second by second, you should for each row (phonecall) see what other phone calls were active at that time. After that you group all of the results by the row's ID, and check which has the maximum count. So basically something like this:

SELECT MAX(calls.count)
FROM (
    SELECT a.id, COUNT(*) AS count
    FROM tbl AS a
    INNER JOIN tbl AS b ON (
        (b.timestamp_start BETWEEN a.timestamp_start AND a.timestamp_end)
        OR
        (b.timestamp_end BETWEEN a.timestamp_start AND a.timestamp_end)
    )
    GROUP BY a.id
) AS calls

Creating an index on the timestamp columns will help as well.

like image 123
reko_t Avatar answered Oct 07 '22 14:10

reko_t