2011-03-29 50 views
1

抱歉,如果已經發布了,但是我已經通過了前幾天的無數帖子,但還是無法獲得我想要的結果。如何將這個數據集轉換爲每個問題的列數

背景:

總之,我開發了一套將動態存儲問卷調查表。 我不會詳細介紹它可能不是相對的。

我基本上想查詢存儲設置問題的用戶輸入的表。 這些問題岔開對方讓我顯示每個問題等

列和行反正這個查詢:

SELECT qr.*, Question 
    FROM QuestionRecord qr 
    INNER JOIN 
    QuestionRecord P 
    ON P.ID = qr.ParentQuestionRecordId 
    JOIN Questions q ON q.ID = qr.QuestionID 

產生以下結果集:

ID FormRecordId QuestionId ParentQuestionRecordId Value   Question  
--------------------------------------------------------------------------------------- 
2  1   31   1    Consultancy  Eligible project costs 
3  1   32   2    NULL    Date 
4  1   33   2    25000   Cash Costs £ 
5  1   34   2    NULL    In Kind Costs £ 
6  1   35   2    25000   Total Costs 
7  1   31   1    Orchard day x2 Eligible project costs 
8  1   32   7    NULL    Date 
9  1   33   7    15000   Cash Costs £ 
10 1   34   7    NULL    In Kind Costs £ 
11 1   35   7    15000   Total Costs 

我基本上要[透視(我認爲)這行看起來像這樣:

Eligible project costs Date  Cash Costs £  In Kind Costs Total Costs 
-------------------------------------------------------------------------------- 
Consultancy    NULL  25000   NULL   25000 
Orchard day x2   NULL  15000   NULL   15000 

我曾嘗試:

SELECT [Eligible project costs],[Date],[Cash Costs £],[In Kind Costs £],[Total Costs] 
FROM 
(
    SELECT qr.*, Question 
    FROM QuestionRecord qr 
    INNER JOIN 
    QuestionRecord P 
    ON P.ID = qr.ParentQuestionRecordId 
    JOIN Questions q ON q.ID = qr.QuestionID  
)pvt 

PIVOT 
(
    MIN(Value) 
    FOR Question IN 
    ([Eligible project costs],[Date],[Cash Costs £],[In Kind Costs £],[Total Costs]) 
)pivotTable 

但這返回上一個單獨的行每一列:

Eligible project costs Date  Cash Costs £  In Kind Costs Total Costs 
-------------------------------------------------------------------------------- 
Consultancy    NULL  NULL    NULL    NULL   
NULL      NULL  NULL    NULL    NULL 
NULL      NULL  25000    NULL    NULL 
NULL      NULL  NULL    NULL    NULL 
NULL      NULL  NULL    NULL   25000 

所以這是親如我設法得到它,我想知道,如果你們/女孩可以幫助我出:) :)

謝謝!

回答

1

嘗試腳本以下更改(刪除線=刪除,大膽加入=):

SELECT [Eligible project costs],[Date],[Cash Costs £],[In Kind Costs £],[Total Costs] 
FROM 
(
    SELECT 
             
  
    qr.*, 
   
     grp = ROW_NUMBER() OVER (PARTITION BY qr.QuestionId ORDER BY qr.ID), Value, 
     Question 
    FROM QuestionRecord qr 
    INNER JOIN 
    QuestionRecord P 
    ON P.ID = qr.ParentQuestionRecordId 
    JOIN Questions q ON q.ID = qr.QuestionID  
)pvt 

PIVOT 
(
    MIN(Value) 
    FOR Question IN 
    ([Eligible project costs],[Date],[Cash Costs £],[In Kind Costs £],[Total Costs]) 
)pivotTable 

我認爲,必須給你結果你是經過。

+0

最後,我們改變了,因爲其他一些問題上的表之間的關係。這給了我們一致的關於數據透視表的「ParentQuestionRecordId」組。 (1 1 1 1而不是1 <222)。 但是我仍然試着對舊模式的答案,它確實工作。謝謝! – 2011-03-29 20:54:55

+0

是的,一致的分組是您的查詢中缺失的部分以及我描述中缺少的術語。所以,也請多多指教! – 2011-03-29 21:03:50

0

變更SELECT qr.*, Question變爲SELECT Value, Question。 PIVOT組由剩餘的列組成。

0

你需要的東西,像andriy kinda指出的,是根據你希望他們分組的方式使每個記錄獨一無二的。現在,如果這是一個調查系統,我會猜測你有某種標識來識別記錄屬於誰。它在單獨行上返回的原因是,基於這些ID,每行都有唯一的記錄,您需要將受訪者ID添加到派生表中,並刪除其他ID。

看到我的例子:

declare @table table (ID int identity(1,1), QuestionID int, value varchar(50), Respondent int) 
declare @questions table (QID int, name varchar(50)) 


insert into @questions values (31,'Eligible project costs') 
insert into @questions values (32,'Date') 
insert into @questions values (33,'Cash Costs') 
insert into @questions values (34,'In Kind Costs') 
insert into @questions values (35,'Total Costs') 

insert into @table values (31,'Consultancy',1) 
insert into @table values (32,null,1) 
insert into @table values (33,25000,1) 
insert into @table values (34,null,1) 
insert into @table values (35,25000,1) 
insert into @table values (31,'Orchard day x2',2) 
insert into @table values (32,null,2) 
insert into @table values (33,15000,2) 
insert into @table values (34,null,2) 
insert into @table values (35,15000,2) 



select 
[Eligible project costs],[Date],[Cash Costs],[In Kind Costs],[Total Costs] 

from 
(
select 
    Respondent, 
    q.name, 
    t.Value 
from @table t 
    inner join @questions q 
     on t.QuestionID=QID 
) a 
pivot 
(
    min(Value) 
    for name in ([Eligible project costs],[Date],[Cash Costs],[In Kind Costs],[Total Costs]) 
) p 
+0

啊,對,謝謝。自從我完成SQL以來已經有一段時間了。我需要按照O'Reilly的食譜來工作。如果只有我有足夠的代表投票了你的答案:[ – 2011-03-29 20:57:03

相關問題