如何对 MySQL 中两个表的数学结果求和
How to sum a mathmatical result from two tables in MySQL
不知道该怎么做!
使用MySQL
我需要显示特定 operatorID 每小时的总行程
所以 operatorid '2' 的总 tripsperhour 应该在某处显示为数字 '7'。这个数字“7”是 TripsPerHour 的总和。
SELECT S.routeid, S.operatorid, R.routeid, S.percentage * R.frequency/100 AS TripsPerHour
FROM route_split S, routes R
WHERE R.routeid = S.routeid
AND S.operatorid ='2';
+---------+------------+---------+--------------+
| routeid | operatorid | routeid | TripsPerHour |
+---------+------------+---------+--------------+
| 6 | 2 | 6 | 1.0000 |
| 12 | 2 | 12 | 4.0000 |
| 13 | 2 | 13 | 2.0000 |
+---------+------------+---------+--------------+
3 rows in set (0.00 sec)
您只需要 SUM()
聚合以及 GROUP BY
子句
SELECT S.operatorid, R.routeid,
SUM(S.percentage * R.frequency/100) AS TripsPerHour
FROM route_split S
JOIN routes R
ON R.routeid = S.routeid
GROUP BY S.operatorid, R.routeid
如果您只需要带operatorid=2
的值,则添加
WHERE S.operatorid = 2
之前GROUP BY
如
SELECT R.routeid,
SUM(S.percentage * R.frequency/100) AS TripsPerHour
FROM route_split S
JOIN routes R
ON R.routeid = S.routeid
WHERE S.operatorid = 2
GROUP BY R.routeid
更新:可以添加SUM() OVER ()
window功能,前提是你的数据库版本是8.0+
,比如
SELECT S.operatorid, R.routeid,
SUM(S.percentage * R.frequency/100) OVER () AS Total_TripsPerHour
FROM route_split S
JOIN routes R
ON R.routeid = S.routeid
WHERE S.operatorid = 2
您还可以使用 WITH ROLLUP 选项将累计总和作为单独的行获取:
SELECT S.operatorid, R.routeid,
SUM(S.percentage * R.frequency/100) AS TripsPerHour ) FROM
route_split S JOIN routes R
ON R.routeid = S.routeid GROUP BY S.operatorid, R.routeid WITH ROLLUP
无论您选择哪个答案,请在将其投入生产之前对其进行解释。
不知道该怎么做!
使用MySQL
我需要显示特定 operatorID 每小时的总行程
所以 operatorid '2' 的总 tripsperhour 应该在某处显示为数字 '7'。这个数字“7”是 TripsPerHour 的总和。
SELECT S.routeid, S.operatorid, R.routeid, S.percentage * R.frequency/100 AS TripsPerHour
FROM route_split S, routes R
WHERE R.routeid = S.routeid
AND S.operatorid ='2';
+---------+------------+---------+--------------+
| routeid | operatorid | routeid | TripsPerHour |
+---------+------------+---------+--------------+
| 6 | 2 | 6 | 1.0000 |
| 12 | 2 | 12 | 4.0000 |
| 13 | 2 | 13 | 2.0000 |
+---------+------------+---------+--------------+
3 rows in set (0.00 sec)
您只需要 SUM()
聚合以及 GROUP BY
子句
SELECT S.operatorid, R.routeid,
SUM(S.percentage * R.frequency/100) AS TripsPerHour
FROM route_split S
JOIN routes R
ON R.routeid = S.routeid
GROUP BY S.operatorid, R.routeid
如果您只需要带operatorid=2
的值,则添加
WHERE S.operatorid = 2
之前GROUP BY
如
SELECT R.routeid,
SUM(S.percentage * R.frequency/100) AS TripsPerHour
FROM route_split S
JOIN routes R
ON R.routeid = S.routeid
WHERE S.operatorid = 2
GROUP BY R.routeid
更新:可以添加SUM() OVER ()
window功能,前提是你的数据库版本是8.0+
,比如
SELECT S.operatorid, R.routeid,
SUM(S.percentage * R.frequency/100) OVER () AS Total_TripsPerHour
FROM route_split S
JOIN routes R
ON R.routeid = S.routeid
WHERE S.operatorid = 2
您还可以使用 WITH ROLLUP 选项将累计总和作为单独的行获取:
SELECT S.operatorid, R.routeid,
SUM(S.percentage * R.frequency/100) AS TripsPerHour ) FROM
route_split S JOIN routes R
ON R.routeid = S.routeid GROUP BY S.operatorid, R.routeid WITH ROLLUP
无论您选择哪个答案,请在将其投入生产之前对其进行解释。