簡體   English   中英

從一個表訪問SQL查詢以進行SELECT並插入另一個表

[英]Access SQL query to SELECT from one table and INSERT into another

以下是我的查詢。 Access不喜歡它,給我Syntax error (missing operator) in query expression 'answer WHERE question = 1'.的錯誤Syntax error (missing operator) in query expression 'answer WHERE question = 1'.

希望您能看到我正在嘗試做的事情。 請特別注意SELECT語句下的第三,第四和第五行。

INSERT INTO Table2 (respondent,1,2,3-1,3-2,3-3,4,5)
SELECT respondent,
answer WHERE question = 1,
answer WHERE question = 2,
answer WHERE answer = 'text 1' AND question = 3,
answer WHERE answer = 'text 2' AND question = 3,
answer WHERE answer = 'text 3' AND question = 3,
answer WHERE question = 4,
longanswer WHERE question 5 FROM Table1 GROUP BY respondent;

更新:

我在這方面取得了一些進展,但是仍然無法獲得我真正想要的格式的數據。 我使用了一些Iif語句來達到目前的效果,但是GROUP BY根本無法按我期望的方式工作。 我還在SELECT語句上嘗試過變體(例如SELECT DISTINCT TOP 100 PERCENTTRANSFORM ),但是我猜我沒有正確使用它們,因為我總是會出錯。 這是我現在的數據:

替代文字

我現在要做的就是將所有相似的respondent行粉碎在一起(即,具有相同編號的respondent行),以便刪除所有空單元格。

編輯:我不確定這是否是您要尋找的

我認為您要執行的操作是這樣(您不能在SELECT部分​​中找到WHERE):

INSERT INTO Table2 (respondent,1,2,3-1,3-2,3-3,4,5)
SELECT respondent,
Iif(question = 1, answer, 0),
Iif(question = 2, answer, 0),
Iif(answer = 'text 1' AND question = 3, answer, 0),
Iif(answer = 'text 2' AND question = 3, answer, 0),
Iif(answer = 'text 3' AND question = 3, answer, 0),
Iif(question = 4, answer)
Iif(question 5 NOT IS NULL, longanswer)
FROM Table1 GROUP BY respondent;

我認為問題5會起作用,但不能完全確定,我認為是正確的。

如果那是您想要的,您應該能夠用NULL替換0。

“將所有相似的響應者行粉碎在一起(也就是說,具有相同編號的響應者行),以便刪除所有空單元格”的方式很簡單:使用GROUP BY。

假設(您沒有給我們提供模式)源表的模式看起來像這樣:

create table response
(
  respondent_id int          not null , -- PK.1 respondent
  question_id   int          not null , -- PK.2 question number
  answer        varchar(200)     null ,

  primary key clustered ( respondent_id , question_id ) ,

)

也就是說,每個響應者最多只能對一個特定問題做出一個響應,然后您要獲取所需結果集的select語句將類似於以下內容(在Transact-SQL中-Access SQL看起來會有所不同:

select respondent_id = t.respondent_id ,
       q1            = max( q1.answer  ) ,
       q2            = max( q2.answer  ) ,
       q3a           = max( q3a.answer ) ,
       q3b           = max( q3b.answer ) ,
       q3c           = max( q3c.answer ) ,
       q4            = max( q4.answer  ) ,
       q5            = max( q5.answer  )
from ( select distinct respondent_id from response ) t
left join response q1  on q1.respondent_id  = t.respondent_id and q1.question_id  = 1
left join response q2  on q2.respondent_id  = t.respondent_id and q2.question_id  = 2
left join response q3a on q3a.respondent_id = t.respondent_id and q3a.question_id = 3 and q3a.answer = 'q3a answer'
left join response q3b on q3b.respondent_id = t.respondent_id and q3b.question_id = 3 and q3b.answer = 'q3b answer'
left join response q3c on q3c.respondent_id = t.respondent_id and q3c.question_id = 3 and q3c.answer = 'q3c answer'
left join response q4  on q4.respondent_id  = t.respondent_id and q4.question_id  = 4
left join response q5  on q5.respondent_id  = t.respondent_id and q5.question_id  = 5
group by t.respondent_id

您也可以使用FROM子句中的單個表來完成此操作,因此:

select respondent_id = t.respondent_id ,
       q1            = max( case t.question_id when 1 then t.answer else null end ) ,
       q2            = max( case t.question_id when 2 then t.answer else null end ) ,
       q3a           = max( case when t.question_id = 3 and t.answer = 'q3a answer' then t.answer else null end ) ,
       q3b           = max( case when t.question_id = 3 and t.answer = 'q3b answer' then t.answer else null end ) ,
       q3c           = max( case when t.question_id = 3 and t.answer = 'q3c answer' then t.answer else null end ) ,
       q4            = max( case t.question_id when 4 then t.answer else null end ) ,
       q5            = max( case t.question_id when 5 then t.answer else null end )
from response t
group by t.respondent_id

終於把這一切整理好了。 我很早就走在正確的軌道上,但是並沒有完全正確。 在刪除了一些不需要的列並執行了一些重要的格式化之后,我能夠運行以下查詢以產生所需的結果。 感謝您的所有建議。

TRANSFORM Max(Sheet1.[answer]) AS MaxOfanswer
SELECT Sheet1.[respondent], Max(Sheet1.[answer]) AS [Total Of answer]
FROM Sheet1
GROUP BY Sheet1.[respondent]
PIVOT Sheet1.[question];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM