1

我一直在为电话做一个数据库项目,这是图表的快速视图,callid 在 call 表中自动递增,sessionid 在 session 表中自动递增,这种方式在三向呼叫中调用有同一张图。除了 sessionstarttime 和 sessionend time,我在每个字段中都输入了虚构数据

使用 phpmyadmin,

我的问题:我需要进行一次查询,以便为客户提供计费时间,也就是电话号码。

示例电话

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

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

A 应按 1 小时计费

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

C 应按 1 小时计费

另一个示例 A -> B 12:00PM 到 1:00PM A -> C 12:30PM 到 1:30PM

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`

按帐户拨打电话的分钟数(注意呼叫者)

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

按帐户接听电话的分钟数(注意 callreciever)

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

这是架构的 xml 输出

    - <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> 
4

1 回答 1

0

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

让我假设会话中给定电话号码的所有时间都是连续的。也就是说,从 12:00 到 12:15 没有来自 B--> 的呼叫(因为 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

由此,您可以聚合所有会话。

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

于 2012-12-04T19:15:33.683 回答