电话呼叫数据库SQL查询

时间:2012-12-04 18:54:14

标签: sql recursive-query recursive-datastructures

我一直在为一个电话呼叫的数据库项目工作,这里是图表的快速视图,callid在调用表中自动增加,sessionid在会话表中自动递增,这种方式在三方通话调用具有相同的图表。我在除sessionstarttime和sessionend时间之外的每个字段中输入了虚构数据

使用phpmyadmin,

我的问题:我需要做一个查询,这将给我一个客户的计费时间,即一个电话号码。

电话示例

A - > B从中午12:00到下午1:00

B - > C从下午12:30到下午1:30

应该按1小时收费

B应开票1个半小时(1:30)

C应开帐单1小时

另一个例子 A - > B中午12:00至下午1:00 A - > C 12:30 PM到1:30 PM

A应按1 1/2小时(1:30)计费

B应按1小时收费

C应开帐单1小时

以下是给定的数据格式

    - <table name="Account">
      <column name="AccountID">1</column> 
      <column name="AcctHolderNum">617-100-5001</column> 
      <column name="ProviderID">1</column> 
      </table>

    <table name="call">
      <column name="callID">4</column> 
      <column name="callSender">617-719-9000</column> 
      <column name="callReceiver">617-730-8100</column> 
      <column name="callStartTime">2012-11-06 06:44:50</column> 
      <column name="callEndTime">2012-11-06 06:55:50</column> 
      <column name="sessionID">1</column> 

    - <table name="phoneNum">
      <column name="phoneNum">617-300-2000</column> 
      <column name="phoneNumFN">Nigel</column> 
      <column name="phoneNumLN">Thornberry</column> 
      <column name="PhoneAccountID">2</column> 

    - <table name="Provider">
      <column name="ProviderID">1</column> 
      <column name="ProviderName">T-Mobile</column> 
      </table>

    - <table name="session">
      <column name="sessionID">1</column> 
      <column name="sessionStartTime">2012-11-06 06:44:50</column> 
      <column name="sessionEndTime">2012-11-06 06:55:50</column> 

这是ER图

http://i.stack.imgur.com/rrh4B.jpg

这是我开始思考的问题,但是在混乱中淹没我,试图使一个查询适合调用表中的每个可能的输入

    FROM `call` as `call1`, `call` as `call2`, `call` as `call3`
    WHERE `call1.sessionid` = `call2.sessionid` = `call3.sessionid`
    AND <REST OF STUFF>
    UNION /* not union all, but union*/
    SELECT same as above but for three way calls
    FROM `call` as `call1`, `call` as `call2`,
    WHERE `call1.sessionid` = `call2.sessionid`
    AND <REST OF STUFF>
    UNION
    SELECT same as above but for two way calls
    FROM `call`
    WHERE <REST OF STUFF>

此外,还有一些简单的查询供参考

计算每次通话的长度

    SELECT TIMEDIFF(MIN(`callStartTime`), MAX(`callEndTime`)) 
    FROM `call` GROUP BY `callID`

计算每个会话的长度

    SELECT TIMEDIFF(MIN(`callStartTime`), MAX(`callEndTime`)) 
    FROM `call` GROUP BY `sessionID`

按帐号进行的通话时间(注释为callsender)

    SELECT SUM(TIMEDIFF(`callStartTime`, `callEndTime`)) 
    FROM `call`, `Phonenum` 
    WHERE `phoneNum.phoneNum` = `call.callSender`  
    GROUP BY `phoneAccountID`

按帐号

收到的电话会议记录(注意电话会议记录)
    SELECT SUM(TIMEDIFF(`callStartTime`, `callEndTime`)) 
    FROM `call`, `Phonenum` 
    WHERE `phoneNum.phoneNum` = `call.callReciever` GROUP BY `phoneAccountID`

以下是架构的

    - <pma:structure_schemas>
    - <pma:database name="jr_Team5" collation="utf8_general_ci" charset="utf8">
      <pma:table name="Account">CREATE TABLE `Account` ( `AccountID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI Primary Key', `AcctHolderNum` varchar(50) NOT NULL COMMENT 'Account Holder''s Phone Number i.e. "617-100-5001"', `ProviderID` int(11) DEFAULT NULL COMMENT 'Foreign Key from "ProviderID"', PRIMARY KEY (`AccountID`), KEY `AcctHolderNum` (`AcctHolderNum`), KEY `ProviderID` (`ProviderID`), CONSTRAINT `Account_ibfk_1` FOREIGN KEY (`ProviderID`) REFERENCES `Provider` (`ProviderID`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;</pma:table> 
      <pma:table name="call">CREATE TABLE `call` ( `callID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI Primary Key', `callSender` varchar(50) NOT NULL COMMENT 'Phone Number of Caller', `callReceiver` varchar(50) NOT NULL COMMENT 'Phone Number of Reciever', `callStartTime` datetime NOT NULL COMMENT 'Time Call Begins', `callEndTime` datetime NOT NULL COMMENT 'Time Call Ends', `sessionID` int(11) NOT NULL COMMENT 'Foreign Key from "SessionID"', PRIMARY KEY (`callID`), KEY `callSender` (`callSender`), KEY `callReceiver` (`callReceiver`), KEY `sessionID` (`sessionID`), CONSTRAINT `call_ibfk_1` FOREIGN KEY (`callSender`) REFERENCES `phoneNum` (`phoneNum`), CONSTRAINT `call_ibfk_2` FOREIGN KEY (`callReceiver`) REFERENCES `phoneNum` (`phoneNum`), CONSTRAINT `call_ibfk_3` FOREIGN KEY (`sessionID`) REFERENCES `session` (`sessionID`) ) ENGINE=InnoDB AUTO_INCREMENT=61 DEFAULT CHARSET=utf8;</pma:table> 
      <pma:table name="phoneNum">CREATE TABLE `phoneNum` ( `phoneNum` varchar(50) NOT NULL COMMENT 'Phone Number on Record', `phoneNumFN` varchar(50) DEFAULT NULL COMMENT 'First Name of Phone User', `phoneNumLN` varchar(100) DEFAULT NULL COMMENT 'Last Name of Phone User', `PhoneAccountID` int(11) DEFAULT NULL COMMENT 'Foreign Key from "AccountID"', PRIMARY KEY (`phoneNum`), KEY `PhoneAccountID` (`PhoneAccountID`), CONSTRAINT `phoneNum_ibfk_1` FOREIGN KEY (`PhoneAccountID`) REFERENCES `Account` (`AccountID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;</pma:table> 
      <pma:table name="Provider">CREATE TABLE `Provider` ( `ProviderID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI Primary Key', `ProviderName` varchar(50) NOT NULL COMMENT 'Network Provider i.e. "Verizon" or "Sprint"', PRIMARY KEY (`ProviderID`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;</pma:table> 
      <pma:table name="session">CREATE TABLE `session` ( `sessionID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI Primary Key', `sessionStartTime` datetime DEFAULT NULL COMMENT 'Session Begin Time', `sessionEndTime` datetime DEFAULT NULL COMMENT 'Session End Time', PRIMARY KEY (`sessionID`) ) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8;</pma:table> 
      </pma:database>
      </pma:structure_schemas>

感谢您的帮助,如果您想到此数据库可能存在的任何其他复杂查询,我们将不胜感激任何帮助/输入/指示

xml中的Fourway呼叫数据示例

    - <table name="call">
      <column name="callID">40</column> 
      <column name="callSender">617-292-1309</column> 
      <column name="callReceiver">617-300-2000</column> 
      <column name="callStartTime">2012-10-31 09:07:35</column> 
      <column name="callEndTime">2012-10-31 11:07:35</column> 
      <column name="sessionID">7</column> 
      </table>
    - <table name="call">
      <column name="callID">41</column> 
      <column name="callSender">617-300-2000</column> 
      <column name="callReceiver">617-234-1234</column> 
      <column name="callStartTime">2012-10-31 09:37:35</column> 
      <column name="callEndTime">2012-10-31 12:37:35</column> 
      <column name="sessionID">7</column> 
      </table>
    - <table name="call">
      <column name="callID">42</column> 
      <column name="callSender">617-234-1234</column> 
      <column name="callReceiver">617-200-4000</column> 
      <column name="callStartTime">2012-10-31 10:37:35</column> 
      <column name="callEndTime">2012-10-31 11:37:35</column> 
      <column name="sessionID">7</column> 

1 个答案:

答案 0 :(得分:0)

我认为你有两个问题。第一个是确定在会话中分配给每个呼叫者的时间。第二是汇总这些信息。

让我假设会话中给定电话号码的所有时间都是连续的。也就是说,没有来自B的呼叫 - &gt;从12:00到12:15(因为C不会连续)。然后,您可以在会话中获得每个用户的计时:

select c.sessionid, c.caller,
       (max(c.EndTime) - min(c.StartTime)) as dur
from ((select c.sessionid, c.callSender as caller, c.StartTime, c.EndTime
       from call c
      ) union all
      (select c.sessionid, c.callReceiver, c.StartTime, c.EndTime
       from call c
      )
     ) c
     on s.sessionid = c.sessionid
group by c.sessionid, c.caller

然后,您可以聚合所有会话。

如果会话中的呼叫周期不连续,则问题更具挑战性。解决此问题的最佳方法取决于数据库和数据库中可用的功能。