[英]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 PERCENT
和TRANSFORM
),但是我猜我没有正确使用它们,因为我总是会出错。 这是我现在的数据:
我现在要做的就是将所有相似的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.