我正在嘗試編寫一個查詢,從表中提取和轉換數據,然後將這些數據插入到另一個表中。是的,這是一個資料倉儲查詢,我是在 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;