从2个来源中提取信息并进行计数

时间:2014-11-10 13:53:36

标签: php mysql sql sql-server database

我被要求在工作中创建一个系统,记录通过我们的VoIP系统拨打和接听的电话数量。如果我能够改变数据源的结构,我本可以做到这一点,但在这种情况下我不允许改变它,现在我依赖于我要求比我更专业的人。

Datasource 1(使用MSSQL) - 这会记录已登录Intranet的用户及其扩展名:

+-------+----------------+--------------+---------------------+
| ID    | USERNAME       | EXTENSION    | LOGGEDIN            |
+-------+----------------+--------------+---------------------+
| 1     | JESS           | 101          | 2014-11-10 12:00:00 |
+-------+----------------+--------------+---------------------+
| 2     | SARAH          | 203          | 2014-11-10 12:21:00 |
+-------+----------------+--------------+---------------------+
| 3     | FERN           | 124          | 2014-11-10 12:21:31 |
+-------+----------------+--------------+---------------------+
| 4     | SARAH          | 101          | 2014-11-10 13:12:00 |
+-------+----------------+--------------+---------------------+

数据源2(使用MySQL) - 数据源2包含使用VoIP系统发出和接收的所有呼叫

+-------+----------------+---------------+---------------------+
| ID    | SRC            | DST           | TIME                |
+-------+----------------+---------------+---------------------+
| 1     | 101            | 02035654684   | 2014-11-10 12:01:00 |
+-------+----------------+---------------+---------------------+
| 2     | 203            | 02087816328   | 2014-11-10 12:22:00 |
+-------+----------------+---------------+---------------------+
| 3     | 124            | 02078939273   | 2014-11-10 12:23:31 |
+-------+----------------+---------------+---------------------+
| 4     | 101            | 04672738782   | 2014-11-10 13:15:00 |
+-------+----------------+---------------+---------------------+
| 5     | 07863522413    | 101           | 2014-11-10 13:21:00 |
+-------+----------------+---------------+---------------------+
| 6     | 02076352423    | 203           | 2014-11-10 13:25:31 |
+-------+----------------+---------------+---------------------+
| 7     | 07836325726    | 101           | 2014-11-10 13:56:00 |
+-------+----------------+---------------+---------------------+

这是我希望能够打印的内容:

+-------+----------------+--------------+--------------+-------------+
| ID    | USERNAME       | INCOMING     | OUTGOING     | TOTAL       |
+-------+----------------+--------------+--------------+-------------+
| 1     | JESS           | 0            | 1            | 1           |
+-------+----------------+--------------+--------------+-------------+
| 2     | SARAH          | 2            | 2            | 4           |
+-------+----------------+--------------+--------------+-------------+
| 3     | FERN           | 0            | 1            | 1           |
+-------+----------------+--------------+--------------+-------------+

从上表中可以看出,Sarah从扩展203开始,然后在Jess离开后登录到101。她在登录时拨打了203和1个电话,拨打了1个电话,同时登录了101.她还在拨打101时登录了2个电话。

我将在这个项目中使用PHP。理想情况下,我想按照总降额对上面的表格进行排序,但我相信一旦有了构建模块,我就可以自己解决这个问题。

提前感谢您提供的任何帮助。

注意:我考虑过创建一个提取信息的临时表,但我已经被告知这是一个坏主意,因为查询的执行时间以及我需要多长时间提取信息。该脚本将每10秒运行一次,因此我使用的任何查询都需要非常精简。

2 个答案:

答案 0 :(得分:0)

SELECT
   u.ID,
   u.USERNAME,
   coalesce(tSRC.INCOMING,0) as INCOMING,
   coalesce(tDST.OUTGOING,0) as OUTGOING,
   (coalesce(tSRC.INCOMING,0)+coalesce(tDST.OUTGOING,0)) as total
FROM
table1 u
LEFT JOIN (
   SELECT a.SRC as ext , count(a.ID) as INCOMING
   FROM table2 a WHERE CHAR_LENGTH(a.SRC)=3 GROUP BY a.SRC
   ) tSRC ON tSRC.ext = u.EXTENSION
LEFT JOIN (
   SELECT b.SRC as ext , count(b.ID) as OUTGOING
   FROM table2 b WHERE CHAR_LENGTH(b.DST)=3 GROUP BY b.DST
   ) tDST ON tDST.ext = u.EXTENSION

编辑:更好的版本:

SELECT
   u.ID,
   u.USERNAME,
   u.EXTENSION,
   COUNT(distinct a.ID) as INCOMING,
   COUNT(distinct b.ID) as OUTGOING,
   (COUNT(distinct a.ID)+COUNT(distinct b.ID)) as TOTAL
FROM
table1 u
LEFT JOIN table2 a ON u.EXTENSION=a.SRC
LEFT JOIN table2 b ON u.EXTENSION=b.DST
GROUP BY u.EXTENSION 

答案 1 :(得分:0)

$q1="SELECT USERNAME, EXTENSION, unix_timestamp(LOGGEDIN) as t FROM table1 WHERE 1 ORDER BY EXTENSION,LOGGEDIN";

$q2="
(SELECT SRC-0 as EXTENSION, 0 as side, unix_timestamp(`TIME`) as t FROM table2 WHERE CHAR_LENGTH(SRC)=3 )
UNION
(SELECT DST-0 as EXTENSION, 1 as side, unix_timestamp(`TIME`) as t FROM table2 WHERE CHAR_LENGTH(DST)=3 )
ORDER BY EXTENSION, t";


$r1 = $db1->query($q1);
$r2 = $db2->query($q2);

$x1 = $r1->fetch_object();
$x2 = $r2->fetch_object();

$users = array();

$future_x1 = $r1->fetch_object();

while($x1 && $x2)
{
    if( $x1->EXTENSION < $x2->EXTENSION )
    {
        $x1=$future_x1 ;
        $future_x1=$r1->fetch_object();
    }
    elseif( $x1->EXTENSION > $x2->EXTENSION )
    {
        $x2 = $r2->fetch_object();
    }
    elseif( $future_x1 && $future_x1->EXTENSION == $x1->EXTENSION && $x2->t >= $future_x1->t )
    {
        $x1=$future_x1 ;
        $future_x1=$r1->fetch_object();
    }
    else
    {
        @$users[$x1->USERNAME][$x2->side]++;
        $x2 = $r2->fetch_object();
    }
}

print_r($users);

此解决方案假设您能够从PHP连接到这两个数据库,并且几乎可以读取两个表中的所有行并且保存大小等于用户名数的数组。