尝试根据哪个月属于哪个季度的定义来累加月度值(以便可以更新定义):
我正在尝试使用内部联接来实现,但失败了:
SELECT qr, sum(price)
INTO Table3
FROM Table1
INNER JOIN Table2
on Table1.mth = Table2.mth
INNER JOIN Table3
on Table2.qr = Table3.qr
另外我不确定在Table1(qr)中增加一列是否会使事情变得容易些?
最佳答案
如果您的table3已经开始有这4行,而您只想更新table3中的价格以反映每个季度的价格,则可以使用以下查询:
UPDATE Table3 T3,
(SELECT T2.qr,sum(T1.price) as quarterly_price
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.mth = T2.mth
GROUP BY T2.qr) AS Result
SET T3.price = Result.quarterly_price
WHERE T3.qr = Result.qr;
sqlfiddle to Update Table3
如果table3没有这4行,并且您想插入它们,请使用以下查询:
INSERT INTO Table3
SELECT T2.qr,sum(T1.price) as quarterly_price
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.mth = T2.mth
GROUP BY T2.qr;
sqlfiddle to Insert into Table3
编辑:我可以从您的代码中进行一些修改,这在Access的查询中效果很好:
SELECT Table2.qr,sum(Table1.price) as Price_new
INTO Table3
FROM Table1
INNER JOIN Table2 ON Table1.mth = Table2.mth
GROUP BY Table2.qr;
关于mysql - 根据季度定义累加每月值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36659943/