以下是我的查询。 Access不喜欢它,给我错误Syntax error (missing operator) in query expression 'answer WHERE question = 1'.
希望你能看到我想要做的事情。请特别注意SELECT
声明下的第3行,第4行和第5行。
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
行具有相同的数字),以便删除所有空的单元格。
答案 0 :(得分:0)
编辑:我不确定这是否是您要找的
我认为你想要做的就是这个(你在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会起作用但不完全确定,我认为这是正确的。
如果你喜欢这样,你应该可以用0替换0。
答案 1 :(得分: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看起来会有所不同:< / p>
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
答案 2 :(得分:0)
终于把这一切都整理好了。我在正确的轨道上走了很长一段时间,但只是没有完全正确。删除一些不需要的列并执行一些重要的格式化后,我能够运行以下查询以生成我需要的结果。感谢您的所有建议。
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];