用SQL计算电话费用

用SQL计算电话费用,sql,Sql,Q1: 答:电信SQL Server数据库中有两个表–客户和费率,如下所示: 客户 PK CustomerPhoneNumber varchar(15) CustomerType int -the type of customer FK CustomerType int - the type of customer CountryCode varchar(4)

Q1:

答:电信SQL Server数据库中有两个表–客户和费率,如下所示:

客户

PK CustomerPhoneNumber varchar(15) CustomerType int -the type of customer FK CustomerType int - the type of customer CountryCode varchar(4) – the country calling code Rate float - the rate per minute of phone call PK客户电话号码varchar(15) CustomerType int-客户的类型 费率

PK CustomerPhoneNumber varchar(15) CustomerType int -the type of customer FK CustomerType int - the type of customer CountryCode varchar(4) – the country calling code Rate float - the rate per minute of phone call FK CustomerType int-客户的类型 CountryCode varchar(4)–国家调用代码 费率浮动-每分钟通话的费率 国家代码示例:

1 – USA 1809 – Dominican Republic 44 – Great Britain 359 – Bulgaria 1–美国 1809年的今天,多米尼加共和国 44–英国 359-保加利亚 所以美国的电话号码是13104405609

如表所示,费率取决于客户类型和所呼叫的国家/地区

给定呼叫的完整起始和目标电话号码(包括国家代码)及其持续时间(分钟),编写一条SQL语句来计算呼叫成本


为了方便起见,让SQL语句的参数调用@FromPhoneNumber、@ToPhoneNumber、@Duration。

如果总速率是源国家/地区的速率+目标国家/地区的速率

(顺便说一句,从商业模式的角度来看,这毫无意义,因为许多离散利率不会由任何一家公司控制或应用)

但如果是,那么SQL将是:

 Select @Duration *
  ((Select fR.Rate
    From Customers fC Join Rates fR 
      On fR.CustomerType = fC.CustomerType
    Where fC.CustomerPhoneNumber = @FromPhoneNumber) 
    +
   (Select tR.Rate
    From Customers tC Join Rates tR 
      On tR.CustomerType = tC.CustomerType
    Where tC.CustomerPhoneNumber = @ToPhoneNumber))
这是我的看法:

SELECT r.rate * @Duration
  FROM CUSTOMERS c
  JOIN RATES r ON r.customertype = c.customertype
              AND (LEFT(r.countrycode, 1) = LEFT(@ToPhoneNumber, 1)
                OR LEFT(r.countrycode, 2) = LEFT(@ToPhoneNumber, 2)
                OR LEFT(r.countrycode, 3) = LEFT(@ToPhoneNumber, 3)
                OR LEFT(r.countrycode, 4) = LEFT(@ToPhoneNumber, 4))
 WHERE c.customerphonenumber = @FromPhoneNumber
我把电话号码中的
@FromPhoneNumber
作为如何找到特定客户的问题。要了解客户将要收取的费率,您需要了解与客户关联的费率基于:

  • 客户类型
  • 所呼叫号码的
    countrycode
  • 因为
    countrycode
    的数据类型是VARCHAR(4),并且没有定义参数的任何数据类型,所以进行了假设。代码并不完全安全,但其思想是只返回一个速率,因为代码应该是唯一的


    有人能给我解释一下为什么其他答案会把价格加起来吗?从什么时候开始,你打电话双向收费?

    改进你的笔记会有帮助……我猜是作业。它写得肯定像作业一样。我知道我为什么要费心格式化它,明目张胆的作业!我猜“始发地和目的地电话号码”是指有或没有国家代码前缀?架构不足!如果费用同时取决于始发国和目的地国,则费率表必须将这两个国家代码作为关键字。。。(取消按您应该将两个费率相加,在这种情况下,请这样说…)