使用 SQL 服务器 NTILE 函数
using SQL Server NTILE function
我正在使用 CTE select 一组记录。从下面的代码中可以看出,我根据学生的成绩分配分数。使用此 CTE,我需要将一组记录插入满足以下条件的 SourceStudentRiskFactor table。按 fst.ClassKey 分组;对于每个组,select 记录 Sum(Points) 位于第三个四分位数(对于 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
我更改了样本数据,使我更容易理解。别担心,由于我没有更改列名,它应该仍然适用于您的 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
我正在使用 CTE select 一组记录。从下面的代码中可以看出,我根据学生的成绩分配分数。使用此 CTE,我需要将一组记录插入满足以下条件的 SourceStudentRiskFactor table。按 fst.ClassKey 分组;对于每个组,select 记录 Sum(Points) 位于第三个四分位数(对于 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
我更改了样本数据,使我更容易理解。别担心,由于我没有更改列名,它应该仍然适用于您的 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