根据季度定义添加月度值

时间:2016-04-16 03:58:28

标签: mysql access-vba

尝试根据哪个月属于哪个季度的定义添加月度值(因此定义可以更新):

enter image description here

我正在尝试使用内部联接来实现但失败了:

SELECT qr, sum(price)
INTO Table3
FROM Table1
INNER JOIN Table2
on Table1.mth = Table2.mth
INNER JOIN Table3
on Table2.qr = Table3.qr

此外,我不确定表1(qr)中的其他列是否会使事情变得更容易?

1 个答案:

答案 0 :(得分:1)

如果您的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;