开发者

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'.

Hopefully you can see what I am trying to do. Please pay particular attention to 3rd, 4th, and 5th lines under the SELECT statement.

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. 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. Here is what my data looks like now:

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

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 a开发者_如何学Pythonre empty are removed.


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):

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.

You should be able to replace the 0 with NULL if that's what you prefer.


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.

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 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:

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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜