访问SQL查询以从一个表中选择并插入到另一个表中

访问SQL查询以从一个表中选择并插入到另一个表中,sql,sql-server,ms-access,Sql,Sql Server,Ms Access,下面是我的问题。Access不喜欢它,这给了我查询表达式“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 =

下面是我的问题。Access不喜欢它,这给了我查询表达式“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%和TRANSFORM,但我想我没有正确使用它们,因为我总是会出错。以下是我的数据现在的样子:

我现在需要做的就是将所有类似的响应行粉碎在一起,即具有相同编号的响应行,以便删除所有空单元格。

编辑:我不确定这是否是您要查找的

我认为您要做的是,您不能在“选择”部分的任何位置:

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-1可以解决,但不能完全确定,我认为这是正确的


如果您愿意的话,您应该能够将0替换为NULL。

将所有类似的响应行粉碎在一起的方式很简单,即,具有相同编号的响应行,以便删除所有空单元格:使用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 ) ,

)
也就是说,每个回答者对某个特定问题最多有一个回答,那么,在Transact-SQL中,用于获取所需结果集的select语句将类似于此-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];

我得到一个错误,您试图执行的查询未将指定的表达式“IIfquestion=1,answer,0”作为聚合函数的一部分。不确定这意味着什么…我发现GROUPBY语句是导致我出错的原因。但不确定原因。与MySQL不同,在Access和SQL Server中,要使用GROUP BY,您必须有一个聚合函数,并且在这个查询示例中没有聚合函数:sum答辩人、count答辩人等。。如果您为响应者找到了合适的聚合函数,那么您必须在Group By子句中包含所有其他Iif语句。Access和SQL Server要使用Group By,您必须具有聚合函数-不正确,这不是这些产品中的任何一种要求,例如,从T Group By c中选择c;是有效的,也是经常使用的选择c和T的替代方法;经常使用的?我从来没有见过这样一种东西可以替代独特的。是否按c从T组中选择c、d;工作在我看来,这将是一个更具可比性的例子。