2010-02-06 77 views
0

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 

實例國家代碼:

 
1 – USA 
1809 – Dominican Republic 
44 – Great Britain 
359 – Bulgaria 

因此,在美國的電話號碼是13104405609.

正如率取決於客戶類型,並呼籲該國表所示。

鑑於呼叫的完整始發號碼和目的地電話號碼(包括國家代碼)及其持續時間(分鐘),編寫單個SQL語句來計算呼叫成本。

爲方便起見,讓SQL語句的參數被稱爲@FromPhoneNumber,@ToPhoneNumber,@Duration。

+2

改善筆記將有助於... – 2010-02-06 00:38:02

+0

作業嗎? – 2010-02-06 00:45:29

+0

我在猜測作業。這當然寫作功課。 – Aaronaught 2010-02-06 00:46:51

回答

0

如果總稅率爲始發全國房價+目的地國家的速度

(順便說一句,這使得從商業模式的角度來看沒有意義,因爲許多離散率不會控制或施加任何一個公司)

但如果是,那麼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)) 
+0

「anypone公司」? :P – Ponkadoodle 2010-02-06 01:09:05

+0

哈!,我是一個2指typer ...對不起... 2010-02-06 01:23:49

0
SELECT CostofCall = @Duration * Sum(Rate) 
FROM 
    Customers C 
    INNER JOIN Rates R ON C.CustomerType = R.CustomerType 
WHERE 
    C.CustomerPhoneNumber IN (@FromPhoneNumber, @ToPhoneNumber) 
0

這是我的看法:

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是如何找到具體的客戶。爲了找到答案,你需要基於相關客戶什麼利率是什麼利率客戶將要被收取:

  1. customertype
  2. countrycode的數量被稱爲

由於countrycode的數據類型是VARCHAR(4),並且沒有定義參數的任何數據類型 - 都進行了假設。代碼並不完全安全,但其思想是隻有一個速率應該返回,因爲代碼應該是唯一的。

有人可以向我解釋爲什麼其他答案合併費率?從什麼時候開始通過電話向兩個方向收費?