我正在尝试编写一个查询,从表中提取和转换数据,然后将这些数据插入到另一个表中。是的,这是一个数据仓库查询,我是在 MS Access 中执行的。所以基本上我想要一些像这样的查询:
INSERT INTO Table2(LongIntColumn2, CurrencyColumn2) VALUES (SELECT LongIntColumn1, Avg(CurrencyColumn) as CurrencyColumn1 FROM Table1 GROUP BY LongIntColumn1);
我尝试过,但收到语法错误消息。
如果你想这样做,你会怎么做?
P粉9492671212023-10-11 14:11:14
您有两个语法选项:
选项 1
CREATE TABLE Table1 ( id int identity(1, 1) not null, LongIntColumn1 int, CurrencyColumn money ) CREATE TABLE Table2 ( id int identity(1, 1) not null, LongIntColumn2 int, CurrencyColumn2 money ) INSERT INTO Table1 VALUES(12, 12.00) INSERT INTO Table1 VALUES(11, 13.00) INSERT INTO Table2 SELECT LongIntColumn1, Avg(CurrencyColumn) as CurrencyColumn1 FROM Table1 GROUP BY LongIntColumn1
选项 2
CREATE TABLE Table1 ( id int identity(1, 1) not null, LongIntColumn1 int, CurrencyColumn money ) INSERT INTO Table1 VALUES(12, 12.00) INSERT INTO Table1 VALUES(11, 13.00) SELECT LongIntColumn1, Avg(CurrencyColumn) as CurrencyColumn1 INTO Table2 FROM Table1 GROUP BY LongIntColumn1
请记住,选项 2 将创建一个仅包含投影上的列(SELECT 上的列)的表。
P粉3221067552023-10-11 10:09:04
没有“VALUES”,没有括号:
INSERT INTO Table2(LongIntColumn2, CurrencyColumn2) SELECT LongIntColumn1, Avg(CurrencyColumn) as CurrencyColumn1 FROM Table1 GROUP BY LongIntColumn1;