2011-01-12 73 views
0

以下是我的查詢。訪問不喜歡它,給我錯誤Syntax error (missing operator) in query expression 'answer WHERE question = 1'.訪問SQL查詢以從一個表中選擇並插入到另一個表中

希望你能看到我想要做的。請特別注意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; 

UPDATE:

我與這個有點進步,但我仍然無法在格式我真的希望得到我的數據。我使用了幾個Iif語句來獲得目前的效果,但GROUP BY根本無法按我期望的方式工作。我也嘗試過我的SELECT聲明中的變體(如SELECT DISTINCT TOP 100 PERCENTTRANSFORM),但我想我沒有正確使用它們,因爲我總是得到錯誤。這裏是我的數據看起來像現在:

alt text

所有我需要做的是現在粉碎所有類似respondent排在一起(即respondent行具有相同數量的),這樣所有可在細胞空的被刪除。

回答

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

編輯:我不知道,如果這是你在找什麼

我想你想要做的是這個(你不能有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。

+0

我收到錯誤`你試圖執行一個查詢,不包括指定的表達式'IIf(question = 1,answer,0)'作爲聚合函數的一部分。`不知道這是什麼意思... – ubiquibacon 2011-01-12 03:28:31

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 - 訪問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 
相關問題