[英]Access SQL query to SELECT from one table and INSERT into another
Below is my query. 以下是我的查询。 Access does not like it, giving me the error
Syntax error (missing operator) in query expression 'answer WHERE question = 1'.
Access不喜欢它,给我
Syntax error (missing operator) in query expression 'answer WHERE question = 1'.
的错误Syntax error (missing operator) in query expression 'answer WHERE question = 1'.
Hopefully you can see what I am trying to do. 希望您能看到我正在尝试做的事情。 Please pay particular attention to 3rd, 4th, and 5th lines under the
SELECT
statement. 请特别注意
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;
UPDATE: 更新:
I have made a little progress with this, but I still cannot get my data in the format I really want. 我在这方面取得了一些进展,但是仍然无法获得我真正想要的格式的数据。 I used several
Iif
statements to get as far as I am now, but GROUP BY
simply isn't working the way I would expect it to. 我使用了一些
Iif
语句来达到目前的效果,但是GROUP BY
根本无法按我期望的方式工作。 I have also tried variations on my SELECT
statement (like SELECT DISTINCT TOP 100 PERCENT
and TRANSFORM
) but I guess I am not using them correctly because I always get errors. 我还在
SELECT
语句上尝试过变体(例如SELECT DISTINCT TOP 100 PERCENT
和TRANSFORM
),但是我猜我没有正确使用它们,因为我总是会出错。 Here is what my data looks like now: 这是我现在的数据:
All I need to do now is smash all the similar respondent
rows together (that is, respondent
rows that have the same number) so all the cells that are empty are removed. 我现在要做的就是将所有相似的
respondent
行粉碎在一起(即,具有相同编号的respondent
行),以便删除所有空单元格。
EDIT: I'm not sure if this is what you're looking for 编辑:我不确定这是否是您要寻找的
I think what you want to do is this (you can't have WHERE in the SELECT section): 我认为您要执行的操作是这样(您不能在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;
I think the question 5 one will work but not totally sure, I think that's correct. 我认为问题5会起作用,但不能完全确定,我认为是正确的。
You should be able to replace the 0 with NULL if that's what you prefer. 如果那是您想要的,您应该能够用NULL替换0。
The way you "smash all the similar respondent rows together (that is, respondent rows that have the same number) so all the cells that are empty are removed" is simple: you use GROUP BY. “将所有相似的响应者行粉碎在一起(也就是说,具有相同编号的响应者行),以便删除所有空单元格”的方式很简单:使用GROUP BY。
Assuming (you haven't given us the schema) that the schema for your source table looks something like this: 假设(您没有给我们提供模式)源表的模式看起来像这样:
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 ) ,
)
which is to say that each respondent has at most one response to a particular question, then your select statement to get the desired result set will look something like this (in Transact-SQL -- Access SQL will look somewhat different: 也就是说,每个响应者最多只能对一个特定问题做出一个响应,然后您要获取所需结果集的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
You could also do it with a single table in the FROM clause, thus: 您也可以使用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
Finally got this all sorted out. 终于把这一切整理好了。 I was on the right track for a long time, but just wasn't getting it exactly right.
我很早就走在正确的轨道上,但是并没有完全正确。 After removing some unneeded columns and performing some significant formatting I was able to run the following query to produce the results I needed.
在删除了一些不需要的列并执行了一些重要的格式化之后,我能够运行以下查询以产生所需的结果。 Thanks for all your suggestions.
感谢您的所有建议。
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.