如何将此SQL数据透视转换为内连接数据透视图?

时间:2014-11-11 21:04:41

标签: sql sql-server

我有三个表:学生,KS3Assessments和AssessmentSets。

  • 每个学生都有StudentID,FName,SName等。
  • AssessmentSet包含评估的标题,截止日期,必须完成的年份组等。全年创建新的,因此无法在SQL中明确命名其标题/ ID。
  • KS3Assessments记录每个都有一个StudentID,指的是学生 完成了工作,一个引用相关AssessmentSet记录的SetID和一个表示学生所取得成果的'NCLevel'。

我需要一个如下所示的结果概览表:

 - StudentID ¦ FName ¦ SName ¦ Creative Writing #1 ¦ Novel Study ¦ Random Thingy Test ¦ etc. ¦ etc.
 - 072509273 ¦ Adam¦ Adamson¦ 5.5¦ 4.8¦ 6.5¦ etc.¦ etc¦
 - 072509274 ¦ Bob ¦ Bobson¦ 5.8¦ 5.2¦ 7.2¦ etc.¦ etc¦

......所以,在任何时候,教师都可以看到学生到目前为止所做的任何评估都取得了什么成就。

到目前为止,使用pivot,我已经设法得到了这个:

 - StudentID, FName, SName, 147, 146, 154 (These numbers are the SetIDs) 
 - 072509273, Adam, Adamson, 5.5, 4.8, 6.5
 - 072509274, Bob, Bobson, 5.8, 5.2, 7.2

这是我的SQL。我真的很感激有关如何解决这个问题并将其升级以获得我正在寻找的结果的任何想法。我怀疑它会涉及内部连接(或两个),但我仍然无法理解枢轴语法。非常感谢。

DECLARE @cols AS NVARCHAR(MAX),
@query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(SetID) 
                from KS3Assessments
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')

set @query = 'SELECT StudentID, FName, SName' + @cols + ' from 
         (
            select KS3Assessments.StudentID,
              Pupils.FName,
              Pupils.SName,
              KS3Assessments.NCLevel,
              KS3Assessments.SetID                                                                  
            from KS3Assessments inner join Pupils on KS3Assessments.StudentID = Pupils.StudentID
            where Pupils.GroupDesignation = ''8KF/En 14/15''
        ) x
        pivot (max(NCLevel) for SetID in (' + @cols + ') ) p '

execute(@query)

3 个答案:

答案 0 :(得分:2)

DECLARE @cols AS NVARCHAR(MAX),
@query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Title) 
                from AssessmentSet 
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')

set @query = 'SELECT StudentID, FName, SName' + @cols + ' from 
         (
            select KS3Assessments.StudentID,
              Pupils.FName,
              Pupils.SName,
              KS3Assessments.NCLevel,
              AssessmentSet.Title
            from KS3Assessments inner join Pupils on KS3Assessments.StudentID = Pupils.StudentID
        inner join AssessmentSet on KS3Assessments.SetID = AssessmentSet.SetID
            where Pupils.GroupDesignation = ''8KF/En 14/15''
        ) x
        pivot (max(NCLevel) for Title in (' + @cols + ') ) p '

execute(@query)

答案 1 :(得分:1)

尝试一下:

DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX)

SELECT @cols = STUFF((
             SELECT DISTINCT ',' + QUOTENAME(title) 
             FROM AssessmentSet
             FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') 
          ,1,1,'')
SET @query = N'
SELECT StudentID, FName, SName, ' + @cols + ' 
FROM (
    SELECT 
        K.StudentID,
        P.FName,
        P.SName,
        K.NCLevel,
        A.title                                                                 
    FROM KS3Assessments K
    INNER JOIN Pupils P ON K.StudentID = P.StudentID
    INNER JOIN AssessmentSet A ON K.SetID = A.SetID
    WHERE Pupils.GroupDesignation = ''8KF/En 14/15''
) x
PIVOT (MAX(NCLevel) FOR title IN (' + @cols + ') 
) p '

EXECUTE(@query)

Sample SQL Fiddle

示例输出:

| STUDENTID | FNAME |   SNAME | CREATIVE WRITING #1 | NOVEL STUDY | RANDOM THINGY TEST |
|-----------|-------|---------|---------------------|-------------|--------------------|
|  72509273 |  Adam | Adamson |                 5.5 |         4.8 |                6.5 |
|  72509274 |   Bob |  Bobson |                 5.8 |         5.2 |                7.2 |

答案 2 :(得分:0)

我不确定表格是如何相关的,但请在下面尝试。

    DECLARE @cols AS NVARCHAR(MAX),
@query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Title) 
                from KS3Assessments 
                inner join AssessmentSet on KS3Assessments.SetID = AssessmentSet.SetID
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')

set @query = 'SELECT StudentID, FName, SName' + @cols + ' from 
         (
            select KS3Assessments.StudentID,
              Pupils.FName,
              Pupils.SName,
              KS3Assessments.NCLevel,
              KS3Assessments.SetID ,
              AssessmentSet.Title                                                                
            from KS3Assessments inner join Pupils on KS3Assessments.StudentID = Pupils.StudentID
            inner join AssessmentSet on KS3Assessments.SetID = AssessmentSet.SetID
            where Pupils.GroupDesignation = ''8KF/En 14/15''
        ) x
        pivot (max(NCLevel) for Title in (' + @cols + ') ) p '

execute(@query)