2013-06-24 68 views
0

好的,一個正在進行的項目(在MS ACCESS中)我已經計算了每個額外「選項」的數量,由客戶爲一家汽車公司購買。爲此,我創建了以下查詢以將每個選項放入一列中,然後在下一列(爲可讀性和匿名性進行編輯)中總結每個選項的總和。'FROM'語法錯誤涉及巨大的UNION子句

SELECT a.options, Count(*) 
FROM(
SELECT TBL.Des1 AS options FROM TBL UNION ALL 
SELECT TBL.Des2 AS options FROM TBL UNION ALL 
SELECT TBL.Des3 AS options FROM TBL UNION ALL 
SELECT TBL.Des4 AS options FROM TBL UNION ALL 
SELECT TBL.Des5 AS options FROM TBL UNION ALL 
SELECT TBL.Des6 AS options FROM TBL UNION ALL 
SELECT TBL.Des7 AS options FROM TBL UNION ALL 
SELECT TBL.Des8 AS options FROM TBL UNION ALL 
SELECT TBL.Des9 AS options FROM TBL UNION ALL 
SELECT TBL.Des10 AS options FROM TBL UNION ALL 
SELECT TBL.Des11 AS options FROM TBL UNION ALL 
SELECT TBL.Des12 AS options FROM TBL UNION ALL 
SELECT TBL.Des13 AS options FROM TBL) AS a 

INTO TBL_OPTION_ALL 
GROUP BY a.options; 

我的問題是嘗試運行時出現「FROM子句中的語法錯誤」錯誤。在錯誤提示終止時,底部的INTO語句被突出顯示。最初,我用括號分隔了每個SELECT,但後來出現了「來自JOIN子句的語法錯誤」的錯誤,並找到了一個與問題類似的帖子,通過刪除括號修復了這個問題。我原本也只有(...)a來創建別名,但我已將它變成(...) AS a,因爲我不確定在Access中是否創建別名的方法。

我有一些理論在何處我的問題所在(從最有可能勒令至少)

  1. 我使用()的時候,我應該使用[],或
    • 我失蹤在我的一些UNION調用中使用括號,或者
    • 我需要完全不同地組織我的括號,並按照之前的嘗試對UNION進行細分。
  2. 它無法處理這麼多聯盟。如果是這樣的話,我怎麼能構造這個?我需要建立多個查詢嗎?
+0

如果您運行的子查詢(UNION承滴盤),它運行沒有任何錯誤? – Taryn

+0

好看,是的它確實運行沒有錯誤,假設我換出了FROM TBL和AS選項的位置(因爲我將在編輯中注意到) – GMoany

回答

1

語法:

UPDATE

試試這個。這是

Select <your columns> 
Into <destination table> 
From <source table> 

所以,你應該有:

SELECT a.options, Count(*) 
INTO TBL_OPTION_ALL 
FROM(
SELECT TBL.Des1 AS options FROM TBL UNION ALL 
SELECT TBL.Des2 AS options FROM TBL UNION ALL 
SELECT TBL.Des3 AS options FROM TBL UNION ALL 
SELECT TBL.Des4 AS options FROM TBL UNION ALL 
SELECT TBL.Des5 AS options FROM TBL UNION ALL 
SELECT TBL.Des6 AS options FROM TBL UNION ALL 
SELECT TBL.Des7 AS options FROM TBL UNION ALL 
SELECT TBL.Des8 AS options FROM TBL UNION ALL 
SELECT TBL.Des9 AS options FROM TBL UNION ALL 
SELECT TBL.Des10 AS options FROM TBL UNION ALL 
SELECT TBL.Des11 AS options FROM TBL UNION ALL 
SELECT TBL.Des12 AS options FROM TBL UNION ALL 
SELECT TBL.Des13 AS options FROM TBL) AS a 
GROUP BY a.options; 
+0

就是這樣。謝謝。我很驚訝於我完全無視命令的多個SELECT短語。 – GMoany

4

請嘗試下面的代碼,將別名替換爲不是字段。

SELECT a.options, Count(*) 
FROM(
SELECT TBL.Des1 AS options FROM TBL UNION ALL 
SELECT TBL.Des2 AS options FROM TBL UNION ALL 
SELECT TBL.Des3 AS options FROM TBL UNION ALL 
SELECT TBL.Des4 AS options FROM TBL UNION ALL 
SELECT TBL.Des5 AS options FROM TBL UNION ALL 
SELECT TBL.Des6 AS options FROM TBL UNION ALL 
SELECT TBL.Des7 AS options FROM TBL UNION ALL 
SELECT TBL.Des8 AS options FROM TBL UNION ALL 
SELECT TBL.Des9 AS options FROM TBL UNION ALL 
SELECT TBL.Des10 AS options FROM TBL UNION ALL 
SELECT TBL.Des11 AS options FROM TBL UNION ALL 
SELECT TBL.Des12 AS options FROM TBL UNION ALL 
SELECT TBL.Des13 AS optionsFROM TBL) AS a 

INTO TBL_OPTION_ALL 
GROUP BY a.options; 
+0

我交換了子查詢的AS和FROM部分,但是我運行嘗試仍然會出現相同的錯誤。子查詢將無誤地運行,但在FROM子句內不起作用。 – GMoany

+0

我刪除了'AS a'部分,但它沒有幫助錯誤拋出。 – GMoany

+0

嘗試從除第一個之外的所有工會中刪除AS選項。並且您需要作爲派生表名的查詢的「AS」部分。 – HLGEM

1

「作爲選項」必須是列名後,你的子查詢,而不是表名之後。向後

SELECT a.options, Count(*) 
INTO TBL_OPTION_ALL 
FROM(
SELECT TBL.Des1 AS options FROM TBL UNION ALL 
SELECT TBL.Des2 AS options FROM TBL UNION ALL 
SELECT TBL.Des3 AS options FROM TBL UNION ALL 
SELECT TBL.Des4 AS options FROM TBL UNION ALL 
SELECT TBL.Des5 AS options FROM TBL UNION ALL 
SELECT TBL.Des6 AS options FROM TBL UNION ALL 
SELECT TBL.Des7 AS options FROM TBL UNION ALL 
SELECT TBL.Des8 AS options FROM TBL UNION ALL 
SELECT TBL.Des9 AS options FROM TBL UNION ALL 
SELECT TBL.Des10 AS options FROM TBL UNION ALL 
SELECT TBL.Des11 AS options FROM TBL UNION ALL 
SELECT TBL.Des12 AS options FROM TBL UNION ALL 
SELECT TBL.Des13 AS options FROM TBL) AS a 
GROUP BY a.options; 
+0

我採取了這一行動,但沒有解決整體問題。它確實使得子查詢實際上做我想要的,但它不會幫助FROM語句中的語法錯誤。 – GMoany

+0

您還需要在最後的UNION ALL之後將子句添加到子查詢中。 – ChrisProsser

+0

是正確的,該條款從我上傳的代碼中缺失,但是我仔細檢查過,它在Access中是正確的。那裏的'FROM'子句不會丟失。 – GMoany