将两列相乘并在内连接上求和

时间:2016-11-09 15:15:47

标签: sql

我有两张桌子,我需要得到a.TOTAL * b.QUANTITY的总和。

aA_IDTOTAL

bB_IDQUANTITY

到目前为止,我写道:

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)作为计算,但它不起作用。我将不胜感激任何帮助!

2 个答案:

答案 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