2010-05-11 64 views
2

我有參與者和答案的基表,結構如下:SQL查詢的問題:如何合併兩個列表

ParticipantId, BusUnitId, QuestionNum, Answer.

在此表中,QuestionNum範圍,比方說,從1到6。我還有另外兩個表,將QuestionNum鏈接到實際問題表,BusUnitQuestions和ParticipantQuestions。對於每個QuestionNum,我都必須根據QuestionId獲取實際的問題文本。

BusUnitId, QuestionId ParticipantId, QuestionId

現在從1至6 BusUnitQuestions假設記錄與QuestionNum有3條記錄,所以QuestionNum 1至3必須加入對QuestionId的從BusUnitQuestions問題,QuestionNum 4到6必須加入到問題在ParticipantQuestions的QuestionId上。我假設我需要在BusUnitQuestions的子查詢中使用ROW_NUMBER()加入到我的答案表中,但之後我就失去了。

如果有人根本理解我,你有什麼建議嗎?

下面是一個示例設置。在此,參與者回答了5個問題(1至5)。前三個問題由參與者的部門設定,最後兩個由參與者選擇。實際上,部門和參與者可以選擇5個以上的問題。我需要將Answers表加入Answers表中,使用AnswersQuestions和ParticipantQuestions中對應於Answers表中QuestionNum的行號。

create table Answers (AnswerId int identity(1,1), ParticipantId int, DeptId int, QuestionNum int, AnswerScore int) 
create table Dept_Question (DqId int identity(1,1), DeptId int, QuestionId int) 
create table Particpant_Question (PqId int identity(1,1), ParticipantId int, QuestionId int) 
create table Questions (QuestionId int identity(1,1), QuestionText nvarchar(200)) 

insert Questions (QuestionText) values ('What is a duck?') 
insert Questions (QuestionText) values ('How much do you weigh?') 
insert Questions (QuestionText) values ('Why does orange fit?') 
insert Questions (QuestionText) values ('Who pokes the fish?') 
insert Questions (QuestionText) values ('Why no cow bells?') 

insert Dept_Question (DeptId, QuestionId) values (3, 3) 
insert Dept_Question (DeptId, QuestionId) values (3, 4) 
insert Dept_Question (DeptId, QuestionId) values (3, 1) 

insert Particpant_Question(ParticipantId, QuestionId) values (1, 2) 
insert Particpant_Question(ParticipantId, QuestionId) values (1, 4) 

insert Answers (ParticipantId, DeptId, QuestionNum, AnswerScore) values (1, 3, 1, 63) 
insert Answers (ParticipantId, DeptId, QuestionNum, AnswerScore) values (1, 3, 2, 89) 
insert Answers (ParticipantId, DeptId, QuestionNum, AnswerScore) values (1, 3, 3, 44) 
insert Answers (ParticipantId, DeptId, QuestionNum, AnswerScore) values (1, 3, 4, 54) 
insert Answers (ParticipantId, DeptId, QuestionNum, AnswerScore) values (1, 3, 5, 72) 
+0

您提供一些樣本DDL? – codingbadger 2010-05-11 07:51:24

+1

要添加到Barry的請求中,請發佈DDL(CREATE TABLE語句),每個表的一些示例數據(希望作爲INSERT語句)以及預期結果。如果我們可以在數據庫中編寫實際的SQL語句並在將其放入答案之前進行調試,那麼人們可以更容易地回答這樣的問題。 – 2010-05-11 07:56:49

+0

我已經添加了一個安裝腳本。 – ProfK 2010-05-11 13:56:58

回答

1

據我瞭解,你有答案的一個列表,但這個問題可以在任何一對夫婦不同的表組成。你可以採取兩種方法來獲得這一個 - 或者是與ISNULL的兩個LEFT JOIN,或者你可以創建一個VIEW,它是你的問題表的一個UNION,然後加入。

如果只是這一個查詢而且未來不會添加更多的問題類型,第一個可能會更容易,但如果您經常這樣做(添加問題類型或查詢數據) ,我會採用方法2,因爲您只需維護視圖而不是原始查詢。

方法1:

SELECT a.ParticipantId, a.QuestionId, a.AnswerId, ISNULL(q1.QuestionText, q2.QuestionText, NULL) as QuestionText 
    FROM Answers a 
    LEFT 
    JOIN Question1 q1 /* BusinessUnitQuestions? */ 
    ON a.QuestionId = q1.QuestionId 
    LEFT 
    JOIN Question2 q2 /* ParticipantQuestions? */ 
    ON a.QuestionId = q2.QuestionId 

這樣一來,問題文本將被無論它出現在表格的拉

方法2:

首先,創建查看所有問題的聯盟,像這樣(根據需要添加更多聯合):

CREATE VIEW AllQuestions 
AS 
SELECT QuestionId, QuestionText 
    FROM Question1 
UNION ALL 
SELECT QuestionId, QuestionText 
    FROM Question2 

然後,您可以使用視圖中的第一個查詢的簡化版本:

SELECT a.ParticipantId, a.QuestionId, a.AnswerId, q.QuestionText 
    FROM Answers a 
    JOIN AllQuestions q 
    ON a.QuestionId = q.QuestionId 
+0

+1,但對於工會a)使用UNION ALL,因爲性能顯着更好b)考慮將問題類型添加爲常量表達式以反映問題出自哪個表 – Unreason 2010-05-11 13:54:44

+0

@Unreason:我已將其更改爲UNION ALL - I忘記UNION需要排序和比較,而UNION ALL則不需要。 – SqlRyan 2010-05-11 14:25:18