无法使用数据透视表制定查询以在单行中组合不同的行值

时间:2014-04-18 14:49:49

标签: sql sql-server stored-procedures pivot

以下是实际表格

Result Table

In the table above:
1)  FEID is the examination ID which remains same for one exam, like ist semester examination of particular class. So it will remain same for all rows in above table as it consists of data of single exam always.
2)  To store result of single student, marks for each subject are entered in each row. So if there are 5 subjects in a class,For each student marks of 5 subjects will be stored in 5 separate rows with marks obtained in each subject
3)  Result, Result_code, NCHMCTID remain same in each row of single student. Like in above table, their values remain same in 3 rows.
Due to some reasons I cant remove this redundancy

So my question is, I need to store result of one student in single row, but number of rows related to single student to store each subject marks is not pre determined(number of subjects can change and determined dynamically)
So , if I have 5 subjects marks in 5 rows, I need those in single row. 

以下是我需要将上表转换为:

enter image description here

以上只有3个科目,但也可以超过3个科目。

为了获得科目列表,我使用下面的查询来获得相同的主题,如: [vb],[c(p)],VB(p)存储在我试图在数据透视表中使用的单个变量中。

DECLARE @values varchar(max);
SET @values = '';
SELECT @values = @values +'['+ CAST(SubjectName AS varchar(max))+ ']' + ','
FROM tbSubjects where SubID IN(Select SubID from tbFinalMarks Where FEID=2) ;
SET @values = SUBSTRING(@values, 1, Len(@values) - 1)

完整程序如下:

ALTER PROCEDURE    [dbo].[prFinalMarksLoadByFEID]

@FEID int 
AS
BEGIN


SET NOCOUNT ON;
DECLARE @values varchar(max);
SET @values = '';
SELECT @values = @values +'['+ CAST(SubjectName AS varchar(max))+ ']' + ','
FROM tbSubjects where SubID IN(Select SubID from tbFinalMarks Where FEID=2) ;
SET @values = SUBSTRING(@values, 1, Len(@values) - 1)
SELECT @values As 'Values'


 select Student_Name,@values,Result,NCHMCTID,Examination_Name from 

    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.

(SELECT     dbo.tbStudent.Name AS Student_Name, dbo.tbSubjects.SubjectName AS Subject_Name, dbo.tbFinalMarks.MarksObtained AS Marks_Obtained, 
                      dbo.tbFinalMarks.Result, dbo.tbFinalMarks.ResultCode AS Result_Code, ISNULL(dbo.tbStudent.NCHMCTID, 'Not Available') AS NCHMCTID, 
                      dbo.tbFinalExam.ExaminationName as Examination_Name
FROM         dbo.tbFinalMarks INNER JOIN
                      dbo.tbSubjects ON dbo.tbFinalMarks.SubID = dbo.tbSubjects.SubID INNER JOIN
                      dbo.tbStudent ON dbo.tbFinalMarks.StdID = dbo.tbStudent.StudentID INNER JOIN
                      dbo.tbFinalExam ON dbo.tbFinalMarks.FEID = dbo.tbFinalExam.FEID  
Where FEID =@FEID
                      ) ps
                      PIVOT
                      (
                        MAX(Marks_Obtained) For Subject_Name IN ([VB],[VB(P)],[C(P)])
                      ) AS pvt

但我无法做到。请帮忙

下面的部分给出了我需要为结果表操作的实际表格

(SELECT     dbo.tbStudent.Name AS Student_Name, dbo.tbSubjects.SubjectName AS Subject_Name, dbo.tbFinalMarks.MarksObtained AS Marks_Obtained, 
                          dbo.tbFinalMarks.Result, dbo.tbFinalMarks.ResultCode AS Result_Code, ISNULL(dbo.tbStudent.NCHMCTID, 'Not Available') AS NCHMCTID, 
                          dbo.tbFinalExam.ExaminationName as Examination_Name
    FROM         dbo.tbFinalMarks INNER JOIN
                          dbo.tbSubjects ON dbo.tbFinalMarks.SubID = dbo.tbSubjects.SubID INNER JOIN
                          dbo.tbStudent ON dbo.tbFinalMarks.StdID = dbo.tbStudent.StudentID INNER JOIN
                          dbo.tbFinalExam ON dbo.tbFinalMarks.FEID = dbo.tbFinalExam.FEID  
    Where FEID =@FEID
                          )

我使用[vb],[vb(p)],[C(P)]代替@values(它包含主题列表),因为在下面的部分使用@值会给我错误:

 PIVOT
                      (
                        MAX(Marks_Obtained) For Subject_Name IN ([VB],[VB(P)],[C(P)])
                      ) AS pvt

以下是数据:

FEID Student_Name Subject_Name Marks_Obtained Result Result_Code NCID Exam_Name     
  2 roof       VB           100       First     1234   ist semester
  2 roof       VB(P)    100       First     1234   ist semester
  2 roof       C(P)     100       First     1234   ist semester
  2 Amir       VB       100       First     nbb 8   ist semester
  2 Amir       VB(P)    100       First     nbb 8   ist semester
  2 Amir       C(P)     100       First     nbb 8   ist semester

1 个答案:

答案 0 :(得分:3)

这是您的查询:

create table #t (FEID int, Student_Name char(4), Subject_Name char(5), Marks_Obtained int, 
Result char(5), Result_Code int, NCID char(5), Exam_Name char(12))
go
insert #t values
(  2, 'roof',       'VB   ',   100,       'First',  NULL,   '1234 ',   'ist semester'),
(  2, 'roof',       'VB(P)',   100,       'First',  NULL,   '1234 ',   'ist semester'),
(  2, 'roof',       'C(P) ',   100,       'First',  NULL,   '1234 ',   'ist semester'),
(  2, 'Amir',       'VB   ',   100,       'First',  NULL,   'nbb 8',   'ist semester'),
(  2, 'Amir',       'VB(P)',   100,       'First',  NULL,   'nbb 8',   'ist semester'),
(  2, 'Amir',       'C(P) ',   100,       'First',  NULL,   'nbb 8',   'ist semester')
go

declare @collist nvarchar(max)
SET @collist = stuff((select distinct ',' + QUOTENAME(subject_name) 
            FROM #t -- your table here
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')
select @collist


declare @q nvarchar(max)
set @q = '
select * 
from (
    select 
    Student_Name, subject_name, Marks_Obtained, Exam_Name, Result, NCID, Result_Code
        from (
        select *
        from #t -- your table here
    ) as x
) as source
pivot (
    max(Marks_Obtained)
    for subject_name in (' + @collist + ')
) as pvt
'

exec (@q)