如何INSERT到从另一个表中提取的表记录我正在尝试编写一个查询,从表中提取和转换数据,然后将这些数据插入另一个表。是的,这是一个数据仓库查询,我在MS Access中这样做。所以基本上我想要一些像这样的查询:INSERT INTO Table2(LongIntColumn2, CurrencyColumn2) VALUES
(SELECT LongIntColumn1, Avg(CurrencyColumn) as CurrencyColumn1 FROM Table1 GROUP BY LongIntColumn1);我试过但得到语法错误消息。如果你想这样做,你会怎么做?
3 回答
智慧大石
TA贡献1946条经验 获得超3个赞
没有“价值”,没有括号:
INSERT INTO Table2(LongIntColumn2, CurrencyColumn2)SELECT LongIntColumn1, Avg(CurrencyColumn) as CurrencyColumn1 FROM Table1 GROUP BY LongIntColumn1;
慕侠2389804
TA贡献1719条经验 获得超6个赞
您有两种语法选项:
选项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 Table2SELECT 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 CurrencyColumn1INTO Table2FROM Table1GROUP BY LongIntColumn1
请记住,选项2将创建一个只包含投影列(SELECT上的列)的表。
绝地无双
TA贡献1946条经验 获得超4个赞
删除VALUES和括号。
INSERT INTO Table2 (LongIntColumn2, CurrencyColumn2)SELECT LongIntColumn1, Avg(CurrencyColumn) FROM Table1 GROUP BY LongIntColumn1
- 3 回答
- 0 关注
- 746 浏览
添加回答
举报
0/150
提交
取消