简体   繁体   English

PIVOT不在一行上生成数据

[英]PIVOT not producing data on a single row

I am trying to write PIVOT to generate a row of data that originally sits as multiple rows in the DB. 我正在尝试编写PIVOT来生成最初位于数据库中的多行数据。 The DB data looks like this (appended) DB数据如下所示(附加)

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

What I want to get to is 1 row for each subject and SchoolID with the grades listed as columns. 我想要获得的是每门科目和SchoolID有1行,并且成绩列为列。

I have written the following 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, 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)

However, this is producing this 但是,这正在产生

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

What I want is 我想要的是

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

Is there a way to get it like this. 有没有办法像这样得到它。

I have never tried a PIVOT before, this is my first time, so all help welcome. 我以前从未尝试过PIVOT,这是我的第一次,欢迎大家的帮助。

As per the comment above - the solution was: 根据上面的评论-解决方案是:

try stripping the inner select down to only the columns that will be used in the pivot and are expected in the output. 尝试将内部选择剥离为仅将在数据透视表中使用并且在输出中预期的列。 intSubjectID, txtSchoolID, intGradeTransposeValue, and intGradeID. intSubjectID,txtSchoolID,intGradeTransposeValue和intGradeID。 all other columns will act as a grouping column in the output and can cause this type of non grouped output. 所有其他列将在输出中充当分组列,并可能导致这种类型的未分组输出。

pivot can't return such what you asking for, but you can use another approach: 枢轴无法返回您要求的内容,但是您可以使用另一种方法:

--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 测试在这里

I think the reason you got the unexpected result is you have so many unwanted columns in the Select in the sub-query and the pivot will group them, too. 我认为您得到意外结果的原因是您在子查询的“ Select中有太多不需要的列,并且pivot也将它们分组。

Your query might be very close to your ideal result: try: 您的查询可能非常接近理想的结果:请尝试:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM