简体   繁体   English

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

[英]How do I turn this SQL pivot into an inner join pivot?

I have three tables: Pupils, KS3Assessments and AssessmentSets. 我有三个表:学生,KS3Assessments和AssessmentSets。

  • Pupils each have a StudentID, FName, SName etc. 每个学生都有一个StudentID,FName,SName等。
  • AssessmentSet contains the title of the assessment, the deadline, the year group that must complete it, etc. New ones are created throughout the year, so their titles/ids can't be named explicitly in the SQL. AssessmentSet包含评估的标题,截止日期,必须完成评估的年份组等。全年都会创建新的评估,因此无法在SQL中明确命名其标题/ ID。
  • KS3Assessments records each have a StudentID that refers to the pupil who completed the work, a SetID that refers to the relevant AssessmentSet record and an 'NCLevel' indicating the result that the pupil achieved. KS3Assessments记录每个都有一个StudentID(它是指完成工作的学生),SetID(它是指相关的评估集记录)和一个“ NCLevel”(表明该学生达到的结果)。

I need a results overview table that looks like this: 我需要一个如下所示的结果概览表:

 - 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¦

... so that, at any time, a teacher can see what a pupil has achieved in whatever assessments they've done so far. ...这样一来,老师可以随时查看学生迄今为止所进行的任何评估所取得的成就。

So far, using pivot, I've managed to get this: 到目前为止,使用数据透视图,我设法做到了:

 - 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

Here's my SQL. 这是我的SQL。 I'd really appreciate any ideas about how to fix this and upgrade it to get the result that I'm looking for. 我非常感谢有关如何解决和升级它以获取所需结果的想法。 I suspect it will involve an inner join (or two), but I'm still having trouble getting my head around the pivot syntax. 我怀疑这将涉及一个(或两个)内部联接,但是我仍然难以理解枢轴语法。 Many thanks. 非常感谢。

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)

try 尝试

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)

Give this a try: 试试看:

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 示例SQL提琴

Sample output: 样本输出:

| 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 |

I am not sure how tables are related but give below try. 我不确定表之间是如何关联的,但请在下面尝试。

    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)

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

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