使用SQL Server NTILE函数

时间:2015-03-09 20:22:47

标签: sql sql-server tsql common-table-expression

我正在使用CTE来选择一组记录。从下面的代码中可以看出,我根据学生的成绩分配了一个分数。使用此CTE,我需要在SourceStudentRiskFactor表中插入一组满足以下条件的记录。由fst.ClassKey分组;对于每个组,选择Sum(Points)在第3个四分位数中的记录(对于ntile(4)。任何例子都将受到高度赞赏。

with AtRiskBarExam as (
    select
        fr.PersonalIdentifier,
        fr.StudentLevelKey,
        dimGrade.SourceKey,
        case 
            when dimGrade.SourceKey = 'A' then 2
            when dimGrade.SourceKey = 'B' then 1
            when dimGrade.SourceKey = 'C' then 0.5
            else 0
        end AS Points
    from
        Final.FactRegistration fr
        inner join  Final.DimTerm dimTerm
            on  dimTerm.TermKey = fr.TermKey and
                fr.VersionKey = 1
        inner join  Final.DimGrade dimGrade
            on  dimGrade.GradeKey = fr.GradeKey 
        inner join Final.DimStudentLevel dimStudentLevel
            on dimStudentLevel.StudentLevelKey = fr.StudentLevelKey and
            dimStudentLevel.SourceKey = 'GR'
    group by
        fr.PersonalIdentifier,
        fr.StudentLevelKey,
        dimGrade.SourceKey
)

insert into Stage.SourceStudentRiskFactor(
        PersonalIdentifier,
        StudentLevelCode,
        StudentTermCode,
        RiskFactorCode)
    select
        fst.PersonalIdentifier,
        fst.StudentLevelCode,
        fst.StudentTermCode,
        'AtRiskBarExam'
    from
        Final.FactStudentTerm fst
        inner join  Final.DimTerm dimTerm
            on  dimTerm.TermKey = fst.TermKey and
                dimTerm.IncludeTermInLoad = 1 and
                fst.VersionKey = 1
        inner join  AtRiskBarExam e
            on  fst.PersonalIdentifier = e.PersonalIdentifier and
                fst.StudentLevelKey = e.StudentLevelKey
        inner join final.DimClass dimClass
            on dimClass.ClassKey = fst.ClassKey

1 个答案:

答案 0 :(得分:1)

我更改了示例数据,以便让我更容易理解。不用担心,因为我没有更改列名,所以它仍然适用于您的CTE。

WITH YourCte
AS
(
    SELECT  'Student1' AS PersonalIdentifier, 7 as TotalPoints, 'Class1' as MaxClassKey
    UNION ALL
    SELECT 'Student2',1,'Class1'
    UNION ALL
    SELECT 'Student3',3,'Class1'
    UNION ALL
    SELECT 'Student4',6,'Class1'
    UNION ALL
    SELECT 'Student5',3,'Class1'
    UNION ALL
    SELECT 'Student6',4,'Class1'
    UNION ALL
    SELECT 'Student7',9,'Class1'
    UNION ALL
    SELECT 'Student8',1,'Class1'
    UNION ALL
    SELECT 'Student9',1,'Class2'
    UNION ALL
    SELECT 'Student10',3,'Class2'
    UNION ALL
    SELECT 'Student11',6,'Class2'
    UNION ALL
    SELECT 'Student12',3,'Class2'
    UNION ALL
    SELECT 'Student13',4,'Class2'
    UNION ALL
    SELECT 'Student14',9,'Class2'
    UNION ALL
    SELECT 'Student15',1,'Class2'
    UNION ALL
    SELECT 'Student16',1,'Class2'
)

SELECT  *,
        --Partition causes NTILE() to look at the classes as individual groups
        NTILE(4) OVER (PARTITION BY MaxClassKey ORDER BY TotalPoints) ClassQuartile
FROM YourCte

结果:

PersonalIdentifier TotalPoints MaxClassKey ClassQuartile
------------------ ----------- ----------- --------------------
Student2           1           Class1      1
Student8           1           Class1      1
Student5           3           Class1      2
Student3           3           Class1      2
Student6           4           Class1      3
Student4           6           Class1      3
Student1           7           Class1      4
Student7           9           Class1      4
Student9           1           Class2      1
Student15          1           Class2      1
Student16          1           Class2      2
Student12          3           Class2      2
Student10          3           Class2      3
Student13          4           Class2      3
Student11          6           Class2      4
Student14          9           Class2      4