我创建了三个表
路线
routeNo decimal
routeName varchar
//每条路线都有多个站点,每个站点都存储为纬度/经度
停止:
routeNo decimal
stopNo decimal
latitude decimal
longitude decimal
和
//订单有一个包含停靠点的路径
orderDelivery:
routeNo decimal
stopNo decimal
orderNo decimal
//为每个司机分配了路线和车辆
工作日:
workday date
driver char(9)
route decimal 4,0
vehicle char(7)
我必须知道,所有司机在几天停止了 超过每日平均停车次数(由所有车手制造)
我知道我需要加入这三个表才能获得所有必需的信息。并编码找到最常访问的每个司机停止,因为我认为这将有助于找到它们可能超过平均停留次数的位置。
到目前为止我的代码:
SELECT t1.driver, t2.stopNo, t1.maxStop
FROM
(
SELECT t.driver, MAX(t.stopCount) AS maxStop
FROM
(
SELECT w.driver, s.stopNo, COUNT(s.stopNo) AS stopCount
FROM workday w INNER JOIN Stops s ON w.route = s.routeNo
INNER JOIN orderDelivery od ON s.routeNo = od.routeNo AND s.stopNo = od.stopNo
GROUP BY w.driver, s.stopNo
) t
GROUP BY t.driver
) t1
INNER JOIN
(
SELECT w.driver, s.stopNo, COUNT(s.stopNo) AS stopCount
FROM workday w INNER JOIN Stops s ON w.route = s.routeNo
INNER JOIN orderDelivery od ON s.routeNo = od.routeNo AND s.stopNo = od.stopNo
GROUP BY w.driver, s.stopNo
) t2
ON t1.driver = t2.driver AND t1.maxStop = t2.stopCount
答案 0 :(得分:0)
这是一个刺:
select w.workday, count(*) as dailyCount
from
workday w inner join
stops s on s.routeNo = w.route inner join
orderDelivery d on d.routeNo = s.routeNo and r.stopNo = s.routeNo
group by w.workday
having count(*) /* daily count */ > (
select avg(dailyCount) /* average across all days for all drivers */
from (
select count(*) as dailyCount
from
workday w inner join
stops s on s.routeNo = w.route inner join
orderDelivery d on d.routeNo = s.routeNo and r.stopNo = s.routeNo
group by w.workday
) dc
)