PIVOT不在一行上生成数据

时间:2018-10-09 12:05:35

标签: tsql pivot

我正在尝试编写PIVOT以生成最初位于数据库中的多行数据。数据库数据看起来像这样(附加)

txtSchoolID txtSubjectArchivedName  intSubjectID    intGradeID  intGradeTransposeValue
95406288448    History                  7             634           2
95406288448    History                  7             635           2
95406288448    History                  7             636           2
95406288448    History                  7             637           2
95406288448    History                  7             638           2
95406288448    History                  7             639           2
95406288448    History                  7             640           2
95406288448    History                  7             641           2
95406288448    History                  7             642           2
95406288448    History                  7             643           2

我想要获得的是每门科目和SchoolID有1行,并且成绩列为列。

我写了以下要点:

SELECT        intSubjectID, txtSchoolID, [636] AS Effort, [637] AS Focus, [638] AS Participation, [639] AS Groupwork, [640] AS Rigour, [641] AS Curiosity, [642] AS Initiative,
                [643] AS SelfOrganisation, [644] as Perserverance               
FROM         (SELECT txtSchoolID, intReportTypeID, txtSubjectArchivedName, intSubjectID, intReportProgress, txtTitle, txtForename, txtPreName, txtMiddleNames, 
                txtSurname, txtGender, txtForm, intNCYear, txtSubmitByTitle, txtSubmitByPreName, txtSubmitByFirstname, txtSubmitByMiddleNames, 
                txtSubmitBySurname, txtCurrentSubjectName, txtCurrentSubjectReportName, intReportCycleID, txtReportCycleName, intReportCycleType, 
                intPreviousReportCycle, txtReportCycleShortName, intReportCycleTerm, intReportCycleAcademicYear, dtReportCycleStartDate, 
                dtReportCycleFinishDate, dtReportCyclePrintDate, txtReportTermName, dtReportTermStartDate, dtReportTermFinishDate,  
                intGradeID, txtGradingName, txtGradingOptions, txtShortGradingName, txtGrade, intGradeTransposeValue FROM VwReportsManagementAcademicReports) p
PIVOT
(MAX        (intGradeTransposeValue)
FOR intGradeID IN ([636], [637], [638], [639], [640], [641], [642], [643], [644] )
) AS pvt
WHERE        (intReportCycleID = 142) AND (intReportProgress = 1)

但是,这正在产生这种情况

intSubjectID    txtSchoolID Effort  Focus   Participation   Groupwork   Rigour  Curiosity   Initiative  SelfOrganisation    Perserverance
8   74001484142 NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
8   74001484142 NULL    NULL    NULL    NULL    NULL    2   NULL    NULL    NULL
8   74001484142 3   NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
8   74001484142 NULL    2   NULL    NULL    NULL    NULL    NULL    NULL    NULL
8   74001484142 NULL    NULL    NULL    2   NULL    NULL    NULL    NULL    NULL
8   74001484142 NULL    NULL    NULL    NULL    NULL    NULL    2   NULL    NULL
8   74001484142 NULL    NULL    2   NULL    NULL    NULL    NULL    NULL    NULL
8   74001484142 NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    2
8   74001484142 NULL    NULL    NULL    NULL    2   NULL    NULL    NULL    NULL
8   74001484142 NULL    NULL    NULL    NULL    NULL    NULL    NULL    2   NULL

我想要的是

intSubjectID    txtSchoolID Effort  Focus   Participation   Groupwork   Rigour  Curiosity   Initiative  SelfOrganisation    Perserverance
8               74001484142  3       2            2           2            2        2       2              2                          2

有没有办法像这样得到它。

我以前从未尝试过PIVOT,这是我第一次,所以欢迎大家光临。

3 个答案:

答案 0 :(得分:0)

根据上面的评论-解决方案是:

尝试将内部选择剥离为仅将在数据透视图中使用且在输出中预期的列。 intSubjectID,txtSchoolID,intGradeTransposeValue和intGradeID。所有其他列将在输出中充当分组列,并可能导致这种类型的未分组输出。

答案 1 :(得分:0)

pivot无法返回您所要求的内容,但是您可以使用另一种方法:

--test dataset
declare @test as table
( txtSchoolID bigint,
  txtSubjectArchivedName varchar(10),
  intSubjectID int,
  intGradeID int,
  intGradeTransposeValue int)
insert into @test 
Values 
(95406288448,'History',7,634,2),
(95406288448,'History',7,635,2),
(95406288448,'History',7,636,2),
(95406288448,'History',7,637,2),
(95406288448,'History',7,638,2),
(95406288448,'History',7,639,2),
(95406288448,'History',7,640,2),
(95406288448,'History',7,641,2),
(95406288448,'History',7,642,2),
(95406288448,'History',7,643,2)

--conditional aggregation
select intSubjectID, 
       txtSchoolID, 
       count(case when intGradeID = 636 then 1 end) AS Effort,
       count(case when intGradeID = 637 then 1 end) AS Focus, 
       count(case when intGradeID = 638 then 1 end) AS Participation, 
       count(case when intGradeID = 639 then 1 end) AS Groupwork, 
       count(case when intGradeID = 640 then 1 end) AS Rigour,
       count(case when intGradeID = 641 then 1 end) AS Curiosity,
       count(case when intGradeID = 642 then 1 end) AS Initiative,
       count(case when intGradeID = 643 then 1 end) AS SelfOrganisation, 
       count(case when intGradeID = 644 then 1 end)  as Perserverance 
from @test
group by intSubjectID, txtSchoolID

test is here

答案 2 :(得分:0)

我认为您得到意外结果的原因是,子查询的Select中有太多不需要的列,而pivot也将它们分组。

您的查询可能与理想结果非常接近:请尝试:

SELECT        intSubjectID, txtSchoolID, [636] AS Effort, [637] AS Focus, [638] AS Participation, [639] AS Groupwork, [640] AS Rigour, [641] AS Curiosity, [642] AS Initiative,
                    [643] AS SelfOrganisation, [644] as Perserverance               
    FROM         (SELECT txtSchoolID, intReportTypeID FROM VwReportsManagementAcademicReports) p  --just these two
    PIVOT
    (MAX        (intGradeTransposeValue)
    FOR intGradeID IN ([636], [637], [638], [639], [640], [641], [642], [643], [644] )
    ) AS pvt
    WHERE        (intReportCycleID = 142) AND (intReportProgress = 1)