枢轴表给出错误的结果

时间:2014-08-05 06:16:06

标签: sql sql-server pivot pivot-table

下面的代码给出了结果集,如第一张图片所示

  SELECT     dbo.tbStudent.Name, dbo.tbStudent.RegNo, dbo.tbFee.PID, dbo.tbFee.Purpose, dbo.tbFee.AmountPaid, dbo.tbFee.StudentID, dbo.tbFee.Date, dbo.tbFee.FeeID, 
                      dbo.tbFee.SemID, dbo.tbFee.CourseID, dbo.tbFee.ModeOfPayment, dbo.tbFee.CheckNo, dbo.tbFee.DDNo, dbo.tbFee.HostelDDNo, dbo.tbFee.FRID, 
                      dbo.tbStudent.Parentage, dbo.tbCourse.Name AS Course, ISNULL(dbo.tbSemester.SemName, ' + @st +') AS Semester
FROM         dbo.tbFee INNER JOIN
                      dbo.tbStudent ON dbo.tbFee.StudentID = dbo.tbStudent.StudentID INNER JOIN
                      dbo.tbCourse ON dbo.tbFee.CourseID = dbo.tbCourse.CourseID LEFT OUTER JOIN
                      dbo.tbSemester ON dbo.tbFee.SemID = dbo.tbSemester.SemID  Where tbFee.SemID=1 

first image

但是使用数据透视表我需要如下结果:

enter image description here

我的数据透视表代码是:

SET @values = '';
 If(@SemID=0)
    BEGIN
        SELECT @values = @values +'['+ CAST(PurPose AS varchar(max))+ ']' + ','
        FROM tbFee Where CourseID=@CourseID
        SET @values = SUBSTRING(@values, 1, Len(@values) - 1)
    END
ELSE
    BEGIN
        SELECT @values = @values +'['+ CAST(PurPose AS varchar(max))+ ']' + ','
        FROM tbFee Where SemID=@SemID
        SET @values = SUBSTRING(@values, 1, Len(@values) - 1)
    END

 Declare @st nvarchar(max)
 set @st='''Not Available''';
 declare @q nvarchar(max)
set @q = '
 Select * from(

SELECT     dbo.tbStudent.Name, dbo.tbStudent.RegNo, dbo.tbFee.PID, dbo.tbFee.Purpose, dbo.tbFee.AmountPaid, dbo.tbFee.StudentID, dbo.tbFee.Date, dbo.tbFee.FeeID, 
                      dbo.tbFee.SemID, dbo.tbFee.CourseID, dbo.tbFee.ModeOfPayment, dbo.tbFee.CheckNo, dbo.tbFee.DDNo, dbo.tbFee.HostelDDNo, dbo.tbFee.FRID, 
                      dbo.tbStudent.Parentage, dbo.tbCourse.Name AS Course, ISNULL(dbo.tbSemester.SemName, ' + @st +') AS Semester
FROM         dbo.tbFee INNER JOIN
                      dbo.tbStudent ON dbo.tbFee.StudentID = dbo.tbStudent.StudentID INNER JOIN
                      dbo.tbCourse ON dbo.tbFee.CourseID = dbo.tbCourse.CourseID LEFT OUTER JOIN
                      dbo.tbSemester ON dbo.tbFee.SemID = dbo.tbSemester.SemID  Where tbFee.SemID=1  
          ) as x
          pivot (
    max(AmountPaid)
    for Purpose in (' + @values + ')
) as pvt
'

exec (@q)

由于行数可以更改的原因,我在@values中获取了目标值列。然而,不是将结果作为具有相同regNo的同一学生的单行,我得到以下结果:

但我得到的是:

enter image description here

1 个答案:

答案 0 :(得分:0)

在PIVOT的源查询中,您应该只指定实际数据透视中涉及的那些列 - 即dbo.tbStudent.Name, dbo.tbStudent.RegNo, dbo.tbFee.Purpose, dbo.tbFee.AmountPaid

SELECT     
dbo.tbStudent.Name, 
dbo.tbStudent.RegNo,
dbo.tbFee.Purpose, 
dbo.tbFee.AmountPaid
FROM         
dbo.tbFee 
INNER JOIN dbo.tbStudent ON dbo.tbFee.StudentID = dbo.tbStudent.StudentID 
INNER JOIN dbo.tbCourse ON dbo.tbFee.CourseID = dbo.tbCourse.CourseID 
LEFT OUTER JOIN dbo.tbSemester ON dbo.tbFee.SemID = dbo.tbSemester.SemID  
Where tbFee.SemID=1

如果存在除此之外的任何其他列,它们将被计入枢轴计算中,并且您将相应地获得多行。