0

以下に説明する3つのテーブルがあります

MonthlyOrder テーブル 4 列 CustomerID DateID (整数で表され、月の初日 = 20121201) OrderCount - INT

HoursSpend Tbale には 4 つの列があります CustomerID DateID Function - VARCHAR() -- 関数は、Reporting、Admini、および Sales の呼び出しです Hours - INTEGER -- その月に各関数に費やされた時間

Rates テーブルには 3 つの列があります CustomerID AccountRate -- Money (IF NULL の場合、デフォルトは $50) OperationRates - Money

このテーブルから達成する必要がある計算は、(LoadCount*AccountRate) + ((HoursSpend テーブルからのレポート関数の時間 + Amin の時間 + セールス コールの時間) * Ratesテーブルからの OperationRates)です。

この計算 を 達成 する ため の最良 の 方法教え
ください
。$18 $50










計算- > (OrderCount*AccountRate) + ((Reporting Hours + Admin Hours + Sales Calls Hours) * OperationRates)
20121201 の customerID 1 の例-> (20*18) + ((2+3+5) * 50) = $860
** トレーニングの時間は含まれていないことに注意

4

1 に答える 1

0

次の列の組み合わせが一意であると仮定すると、MonthlyOrder[CustomerID,DateID]、HoursSpend[CustomerID,DateID,Function]、Rates[CustomerID] のようなものを使用できます。

select mo.CustomerID, mo.DateID, mo.OrderCount*max(AccountRate) +
        (sum(rep.hours)+sum(adm.hours)+sum(sc.hours))*max(rt.OperationRates)
from MonthlyOrder as mo
  join Rates as rt                on rt.CustomerID = mo.CustomerID
  left join HoursSpend as rep     on rep.CustomerID = mo.CustomerID and
                                     rep.DateId = mo.DateId and
                                     rep.Function = 'Reporting'
  left join HoursSpend as adm     on adm.CustomerID = mo.CustomerID and
                                     adm.DateId = mo.DateId and
                                     adm.Function = 'Admin'
  left join HoursSpend as sc      on sc.CustomerID = mo.CustomerID and
                                     sc.DateId = mo.DateId and
                                     sc.Function = 'Sales Calls'
group by mo.CustomerID, mo.DateID, mo.OrderCount
于 2012-12-18T20:37:52.977 に答える