如何将此数据集转换为每个问题的列

时间:2011-03-29 11:15:38

标签: sql sql-server

对不起,如果已经发布了这个帖子,但我在过去的一天中已经过了无数的帖子,但仍然无法得到我想要的结果。

背景:

简而言之,我正在开发一套能够动态存储调查问卷的表格。 我不会详细介绍它可能不是相对的。

我基本上想查询存储设置问题的用户输入的表。 这些问题相互分离,允许我显示每个问题的列和行等。

无论如何这个查询:

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

这就像我设法得到它一样接近,我想知道你们/女孩是否可以帮助我:)

谢谢!

3 个答案:

答案 0 :(得分: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

我认为必须提供你所追求的结果。

答案 1 :(得分:0)

SELECT qr.*, Question更改为SELECT Value, Question。 PIVOT分组由其余列组成。

答案 2 :(得分:0)

你所需要的,就像andriy有点指出的那样,是根据你想要的方式使每个记录独特的东西。现在,如果这是一个调查系统我会猜测你有某种身份证来识别该记录属于谁。它在单独行上返回的原因是你根据这些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