1

我有两张桌子

运算符表

proportion, name, routeid

(比例保持百分比值,例如 100 = 100% 或 50 = 50%,这显示了此运营商运行的服务的百分比)

路由表

routeid, frequency 

(频率保持每小时的列车数量)

我现在需要做的是编写一个查询,该查询将汇总某个运营商名称为 Arriva 的运营商每小时的列车数量。因此,如果 arriva 运行特定服务 1 的 100% 和服务 2 的 50%,它也应该解决这个问题。

我在想它看起来像这个伪代码

All arriva trains per hour = Frequency * (proportion/100)
Where Operators.name = Arriva
4

3 回答 3

0

尝试这个

  select SUM((ot.frequency * rt.proportion)/100.0 )
  from operators_table ot 
    join route_table rt on ot.route_id = rt.route_id
  WHERE ot.name = 'Arriva';
于 2012-11-22T22:35:44.840 回答
0
SELECT SUM(frequency * proportion/100) total
FROM Operators JOIN Route USING (routeid)
WHERE Operators.name = 'Arriva'
于 2012-11-22T22:35:53.503 回答
0
declare @operator table 
(
operatorid int not null identity(1,1) primary key clustered
, name nvarchar(64)
, proportion int--(proportion hold % value e.g 100 = 100% or 50 = 50% this shows the % of services ran by this operator)
, routeid int
)


declare @route table
(
    routeid int not null identity(1,1) primary key clustered
    , frequency int --(frequency holds number of trains per hour)
)

--put your insert statements here

select o.operatorid, o.name, sum(r.frequency * (o.proportion/100)) AllTrainsPerHour
from @operator o
left outer join @route r
on o.routeid = r.routeid
where o.name = 'Arriva'
group by o.operatorid, o.name
于 2012-11-22T22:39:00.420 回答