MySQL 来自 1 个以上的 sum() table
MySQL sum() from more than 1 table
SELECT fee +
sum(((hourEnd-hourStart)*4) as sumAct3
from reserve join activity
on reserve.idactivity=activity.idactivity
join customer
on customer.idcustomer=reserve.idcustomer
where activity.typeAct=1 and customer.idcustomer='S1')
+
sum(((hourEnd-hourStart)*2) as sumAct2
from reserve join activity
on reserve.idactivity=activity.idactivity
join customer
on customer.idcustomer=reserve.idcustomer
where activity.typeAct=2 and customer.idcustomer='S1')
FROM customer
WHERE idcustomer='S1';
好的,所以每个 sum() returns 都是想要的值,我已经检查过了,但是我在尝试将所有内容加在一起然后得到总值时遇到了问题。
基本上我必须这样做:
- 初始费用(是"customer"table上定义的设定值)+activity1成本总和+activity2成本总和==值
但我不知道如何让它工作所以我得到了所有的总和,我尝试了一段时间但我总是得到 "SELECT is not valid at this position"
您只需 JOIN
将 table 彼此 table 并使用条件聚合来获得 activity 总和,即可更轻松地获得小时总和:
SELECT c.idcustomer,
SUM(CASE WHEN a.typeAct = 1 THEN hourEnd - hourStart ELSE 0 END) * 4 AS sumAct1,
SUM(CASE WHEN a.typeAct = 2 THEN hourEnd - hourStart ELSE 0 END) * 2 AS sumAct2
FROM customer c
JOIN reserve r ON r.idcustomer = c.idcustomer
JOIN activity a ON a.idactivity = r.idactivity
WHERE a.typeAct IN (1, 2) AND c.idcustomer = 'S1'
GROUP BY c.idcustomer
然后使用这个结果作为派生的 table 进行最后的加法:
SELECT c.idcustomer, c.fee + f.sumAct1 + f.sumAct2
FROM customer c
JOIN (
SELECT c.idcustomer,
SUM(CASE WHEN a.typeAct = 1 THEN hourEnd - hourStart ELSE 0 END) * 4 AS sumAct1,
SUM(CASE WHEN a.typeAct = 2 THEN hourEnd - hourStart ELSE 0 END) * 2 AS sumAct2
FROM customer c
JOIN reserve r ON r.idcustomer = c.idcustomer
JOIN activity a ON a.idactivity = r.idactivity
WHERE a.typeAct IN (1, 2) AND c.idcustomer = 'S1'
GROUP BY c.idcustomer
) f ON f.idcustomer = c.idcustomer
SELECT fee +
sum(((hourEnd-hourStart)*4) as sumAct3
from reserve join activity
on reserve.idactivity=activity.idactivity
join customer
on customer.idcustomer=reserve.idcustomer
where activity.typeAct=1 and customer.idcustomer='S1')
+
sum(((hourEnd-hourStart)*2) as sumAct2
from reserve join activity
on reserve.idactivity=activity.idactivity
join customer
on customer.idcustomer=reserve.idcustomer
where activity.typeAct=2 and customer.idcustomer='S1')
FROM customer
WHERE idcustomer='S1';
好的,所以每个 sum() returns 都是想要的值,我已经检查过了,但是我在尝试将所有内容加在一起然后得到总值时遇到了问题。
基本上我必须这样做:
- 初始费用(是"customer"table上定义的设定值)+activity1成本总和+activity2成本总和==值
但我不知道如何让它工作所以我得到了所有的总和,我尝试了一段时间但我总是得到 "SELECT is not valid at this position"
您只需 JOIN
将 table 彼此 table 并使用条件聚合来获得 activity 总和,即可更轻松地获得小时总和:
SELECT c.idcustomer,
SUM(CASE WHEN a.typeAct = 1 THEN hourEnd - hourStart ELSE 0 END) * 4 AS sumAct1,
SUM(CASE WHEN a.typeAct = 2 THEN hourEnd - hourStart ELSE 0 END) * 2 AS sumAct2
FROM customer c
JOIN reserve r ON r.idcustomer = c.idcustomer
JOIN activity a ON a.idactivity = r.idactivity
WHERE a.typeAct IN (1, 2) AND c.idcustomer = 'S1'
GROUP BY c.idcustomer
然后使用这个结果作为派生的 table 进行最后的加法:
SELECT c.idcustomer, c.fee + f.sumAct1 + f.sumAct2
FROM customer c
JOIN (
SELECT c.idcustomer,
SUM(CASE WHEN a.typeAct = 1 THEN hourEnd - hourStart ELSE 0 END) * 4 AS sumAct1,
SUM(CASE WHEN a.typeAct = 2 THEN hourEnd - hourStart ELSE 0 END) * 2 AS sumAct2
FROM customer c
JOIN reserve r ON r.idcustomer = c.idcustomer
JOIN activity a ON a.idactivity = r.idactivity
WHERE a.typeAct IN (1, 2) AND c.idcustomer = 'S1'
GROUP BY c.idcustomer
) f ON f.idcustomer = c.idcustomer