我有两张桌子,我需要得到a.TOTAL * b.QUANTITY
的总和。
a
(A_ID
,TOTAL
)
b
(B_ID
,QUANTITY
)
到目前为止,我写道:
SELECT a.A_ID, a.TOTAL * b.QUANTITY as calculation
FROM a INNER JOIN b ON
a.A_ID = b.B_ID
我尝试...SUM(a.TOTAL * b.QUANTITY)
作为计算,但它不起作用。我将不胜感激任何帮助!
答案 0 :(得分:4)
试试这个:
SELECT A_ID, SUM(calculation) as mySum
FROM (
SELECT a.A_ID, a.TOTAL * b.QUANTITY as calculation
FROM a INNER JOIN b ON
a.A_ID = b.B_ID
) q
GROUP BY A_ID
答案 1 :(得分:2)
您是否打算对A的内容进行分组?如果是这样,那么这可能会给你想要的结果:
SELECT a.A_ID,
SUM(a.TOTAL * b.QUANTITY) AS calculation
FROM a
INNER JOIN b
ON a.A_ID = b.B_ID
GROUP BY a.A_ID