我有两张桌子。
a) ai_account
b) ai_order_product
我想为特定的supplier_id做一些计算。
1,totalAmount,我想做点什么 SUM(ai_order_product.quantity * ai_order_product.cost)
2,amountPaid,这是供应商支付的总金额 像SUM(ai_account.amount)这样的东西,参考supplier_id。
3)余额,这将由SUM计算(ai_order_product.quantity * ai_order_product.cost) - SUM(ai_invoice.amount)
4)lastPayment日期,即MAX(ai_account.addDate)。
我尝试过这样的事情。
SELECT SUM(op.quantity * op.cost) as totalAmount,
SUM(ac.amount) as amountPaid,
SUM(op.quantity * op.cost) - SUM(ac.amount) as balance,
MAX(ac.addDate) as lastPayment
FROM ai_order_product op
LEFT JOIN ai_account ac
ON (op.supplier_id = ac.trader_id)
WHERE op.supplier_id = 42
它无法正常工作,它会获取一些意外的值,而上述预期的结果是,
for supplier_id = 42,
1) totalAmount = 1375,
2) amountPaid = 7000,
3) balance = -5625,
4) lastPayment = 2011-11-23
and for supplier_id = 35,
1) totalAmount = 1500,
2) amountPaid = 43221,
3) balance = -41721,
4) lastPayment = 2011-11-28
and for supplier_id = 41
1) totalAmount = 0
2) amountPaid = 3000,
3) balance = -3000,
4) lastPayment = 2011-11-09
我想通过supplier_id获取一行。
P.S:我刚刚输入了一些虚拟值,这就是为什么计算主要是负数,而在应用中,计算值将是正数。
答案 0 :(得分:2)
这是因为每个“ai_order_product”行都被多次计数(ai_account表中每行存在一次)。
试试这个:
SELECT
op.totalAmount as totalAmount
, SUM(ac.amount) as amountPaid
, op.totalAmount - SUM(ac.amount) as balance
, MAX(ac.addDate) as lastPayment
FROM (
select supplier_id, sum(quantity * cost) as totalAmount
from ai_order_product
group by supplier_id) op
LEFT JOIN ai_account ac ON (op.supplier_id = ac.trader_id)
WHERE op.supplier_id = 42
这可能略有偏差,但这种一般逻辑应该有效。
答案 1 :(得分:1)
在 SELECT 语句中使用 SUM 等聚合函数时,必须使用 GROUP BY 。
SELECT op.supplier_id as supplierId,
SUM(op.quantity * op.cost) as totalAmount,
SUM(ac.amount) as amountPaid,
SUM(op.quantity * op.cost) - SUM(ac.amount) as balance,
MAX(ac.addDate) as lastPayment
FROM ai_order_product op
LEFT JOIN ai_account ac
ON (op.supplier_id = ac.trader_id)
GROUP BY op.supplier_id
HAVING supplierId = 42
答案 2 :(得分:1)
SELECT
SUM(op.quantity * op.cost) as totalAmount
, ac2.amountPaid
, SUM(op.quantity * op.cost) - ac2.balance
, ac2.lastPayment
FROM ai_order_product op
LEFT JOIN (SELECT
ac.supplier_id
, MAX(ac.addDate) as lastPayment
, SUM(ac.amount) as balance
FROM ai_account ac
WHERE (op.supplier_id = ac.supplier_id)
GROUP BY ac.supplier_id) ac2 ON (ac2.supplier_id = op.supplier_id)
WHERE op.supplier_id = 42
GROUP BY op.supplier_id
当您选择多个supplier_id时,group by
条款会启动。
答案 3 :(得分:0)
请注意,您的预期值和实际值都加倍了。输入样本数据并运行查询后,我得到(对于supplier_id = 42)
+-------------+------------+---------+-------------+
| totalAmount | amountPaid | balance | lastPayment |
+-------------+------------+---------+-------------+
| 2750 | 14000 | -11250 | 2011-11-23 |
+-------------+------------+---------+-------------+
那是因为每个表中有两行符合连接条件,导致结果加倍。
答案 4 :(得分:0)
我认为您需要首先获取其中一个表的子总计,这样您才能返回1行。因此,内部选择返回最大值和总和,所以当你加入它时,你只得到1行。
SELECT SUM(op.quantity * op.cost) as totalAmount,
ac.addDate as lastPayment,
ac.amount as amountPaid,
SUM(op.quantity * op.cost) - SUM(ac.amount) as balance
FROM ai_order_product op
INNER JOIN (
SELECT max(IaiaddDate) as addDate, sum(iai.amount) as Amount, iai.supplier_ID
FROM ai_account iai
Group by supplier_ID) ac
ON AC.Supplier_ID = Op.Supplier_ID
WHERE op.supplier_id = 42
按Ac.addDate分组,ac.amount,op.supplier_ID - 只是在哪里停止子句。