Skip to content

Commit

Permalink
[BIA-1301]: Update fn_GetStudentGradesSummary to use descriptor const…
Browse files Browse the repository at this point in the history
…ant (#228)
  • Loading branch information
jegasape authored Dec 19, 2022
1 parent c345efd commit c659826
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
-- SPDX-License-Identifier: Apache-2.0
-- Licensed to the Ed-Fi Alliance under one or more agreements.
-- The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
-- See the LICENSE and NOTICES files in the project root for more information.
IF EXISTS (
SELECT 1
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_SCHEMA = 'analytics'
AND ROUTINE_NAME = 'fn_GetStudentGradesSummary'
)
BEGIN
DROP FUNCTION analytics.fn_GetStudentGradesSummary
END
GO

CREATE OR ALTER FUNCTION analytics.fn_GetStudentGradesSummary (
@StudentKey VARCHAR(8000)
,@SchoolKey VARCHAR(8000)
)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @val VARCHAR(8000)

SELECT @val = COALESCE(@VAL, '') + (
CAST(CONCAT (
', '
,CHAR(10)
,course.CourseTitle
,': '
,gradeFact.NumericGradeEarned
) AS VARCHAR(8000))
)
FROM edfi.Student
INNER JOIN edfi.Grade gradeFact
ON gradeFact.StudentUSI = Student.StudentUSI
INNER JOIN edfi.CourseOffering
ON CourseOffering.SchoolId = gradeFact.SchoolId
AND CourseOffering.LocalCourseCode = gradeFact.LocalCourseCode
AND CourseOffering.SchoolYear = gradeFact.SchoolYear
AND CourseOffering.SessionName = gradeFact.SessionName
INNER JOIN edfi.Course
ON Course.CourseCode = CourseOffering.CourseCode
AND Course.EducationOrganizationId = CourseOffering.EducationOrganizationId
INNER JOIN edfi.Descriptor
ON GradeTypeDescriptorId = Descriptor.DescriptorId
INNER JOIN edfi.GradingPeriod
ON gradeFact.GradingPeriodDescriptorId = GradingPeriod.GradingPeriodDescriptorId
AND gradeFact.GradingPeriodSequence = GradingPeriod.PeriodSequence
AND gradeFact.SchoolId = GradingPeriod.SchoolId
AND gradeFact.GradingPeriodSchoolYear = GradingPeriod.SchoolYear
INNER JOIN analytics_config.DescriptorMap
ON DescriptorMap.DescriptorId = GradeTypeDescriptorId
INNER JOIN analytics_config.DescriptorConstant
ON DescriptorConstant.DescriptorConstantId = DescriptorMap.DescriptorConstantId
WHERE DescriptorConstant.ConstantName IN ('GradeType.Semester')
AND Student.StudentUniqueId = @StudentKey
AND gradeFact.SchoolId = @SchoolKey
ORDER BY gradeFact.GradingPeriodDescriptorId DESC
,gradeFact.SchoolId DESC
,GradingPeriod.BeginDate DESC;

RETURN COALESCE(STUFF(COALESCE(@val, ''), 1, 3, ''),'');
END
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
-- SPDX-License-Identifier: Apache-2.0
-- Licensed to the Ed-Fi Alliance under one or more agreements.
-- The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
-- See the LICENSE and NOTICES files in the project root for more information.
IF EXISTS (
SELECT 1
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_SCHEMA = 'analytics'
AND ROUTINE_NAME = 'fn_GetStudentGradesSummary'
)
BEGIN
DROP FUNCTION analytics.fn_GetStudentGradesSummary
END
GO

CREATE OR ALTER FUNCTION analytics.fn_GetStudentGradesSummary (
@StudentKey VARCHAR(8000)
,@SchoolKey VARCHAR(8000)
)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @val VARCHAR(8000)

SELECT @val = COALESCE(@VAL, '') + (
CAST(CONCAT (
', '
,CHAR(10)
,course.CourseTitle
,': '
,gradeFact.NumericGradeEarned
) AS VARCHAR(8000))
)
FROM edfi.Student
INNER JOIN edfi.Grade gradeFact
ON gradeFact.StudentUSI = Student.StudentUSI
INNER JOIN edfi.CourseOffering
ON CourseOffering.SchoolId = gradeFact.SchoolId
AND CourseOffering.LocalCourseCode = gradeFact.LocalCourseCode
AND CourseOffering.SchoolYear = gradeFact.SchoolYear
AND CourseOffering.SessionName = gradeFact.SessionName
INNER JOIN edfi.Course
ON Course.CourseCode = CourseOffering.CourseCode
AND Course.EducationOrganizationId = CourseOffering.EducationOrganizationId
INNER JOIN edfi.Descriptor
ON GradeTypeDescriptorId = Descriptor.DescriptorId
INNER JOIN edfi.GradingPeriod
ON gradeFact.GradingPeriodDescriptorId = GradingPeriod.GradingPeriodDescriptorId
AND gradeFact.GradingPeriodSequence = GradingPeriod.PeriodSequence
AND gradeFact.SchoolId = GradingPeriod.SchoolId
AND gradeFact.GradingPeriodSchoolYear = GradingPeriod.SchoolYear
INNER JOIN analytics_config.DescriptorMap
ON DescriptorMap.DescriptorId = GradeTypeDescriptorId
INNER JOIN analytics_config.DescriptorConstant
ON DescriptorConstant.DescriptorConstantId = DescriptorMap.DescriptorConstantId
WHERE DescriptorConstant.ConstantName IN ('GradeType.Semester')
AND Student.StudentUniqueId = @StudentKey
AND gradeFact.SchoolId = @SchoolKey
ORDER BY gradeFact.GradingPeriodDescriptorId DESC
,gradeFact.SchoolId DESC
,GradingPeriod.BeginDate DESC;

RETURN COALESCE(STUFF(COALESCE(@val, ''), 1, 3, ''),'');
END
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
-- SPDX-License-Identifier: Apache-2.0
-- Licensed to the Ed-Fi Alliance under one or more agreements.
-- The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
-- See the LICENSE and NOTICES files in the project root for more information.
DROP VIEW IF EXISTS analytics.equity_StudentHistoryDim;

CREATE
OR REPLACE VIEW analytics.equity_StudentHistoryDim AS
WITH AttendanceHist AS (
SELECT StudentSchoolKey
,COUNT(1) AS DaysEnrolled
,SUM(ReportedAsAbsentFromHomeRoom) AS DaysAbsent
FROM analytics.chrab_ChronicAbsenteeismAttendanceFact
GROUP BY StudentSchoolKey
)
,GradeList AS (
SELECT CONCAT (
studentSectionDim.StudentKey
,'-'
,studentSectionDim.SchoolKey
) AS StudentSchoolKey
,studentSectionDim.LastModifiedDate
,STRING_AGG(CAST(CONCAT (
studentSectionDim.CourseTitle
,': '
,gradeFact.NumericGradeEarned
) AS VARCHAR(8000)), CONCAT(', ','')
ORDER BY gradeFact.GradingPeriodKey DESC) AS GradeSummary
FROM analytics.ews_StudentSectionGradeFact gradeFact
INNER JOIN analytics.StudentSectionDim studentSectionDim
ON gradeFact.StudentSectionKey = studentSectionDim.StudentSectionKey
AND gradeFact.SectionKey = studentSectionDim.SectionKey
INNER JOIN edfi.descriptor
ON descriptor.codevalue = gradeFact.GradeType
INNER JOIN analytics_config.DescriptorMap
ON DescriptorMap.DescriptorId = descriptor.DescriptorId
INNER JOIN analytics_config.DescriptorConstant
ON DescriptorConstant.DescriptorConstantId = DescriptorMap.DescriptorConstantId
WHERE DescriptorConstant.ConstantName IN ('GradeType.Semester')
GROUP BY studentSectionDim.StudentKey
,studentSectionDim.SchoolKey
,studentSectionDim.LastModifiedDate
)

SELECT DISTINCT studentSchoolDim.StudentKey
,studentSchoolDim.StudentSchoolKey
,COALESCE(gradeFact.GradeSummary,'') AS GradeSummary
,studentSchoolDim.SchoolKey AS CurrentSchoolKey
,COALESCE((CAST((DaysEnrolled - DaysAbsent) AS DECIMAL) / CAST(DaysEnrolled AS DECIMAL) * 100), 100.00) AS AttendanceRate
,(
SELECT COUNT(1)
FROM analytics.equity_StudentDisciplineActionDim discipline
WHERE discipline.StudentSchoolKey = studentSchoolDim.StudentSchoolKey
) AS ReferralsAndSuspensions
,(
SELECT STRING_AGG(CONCAT (
SchoolName
,CASE WHEN(ssd.ExitWithdrawDate is not null) then ' ' else '' END
,COALESCE(CAST(ssd.ExitWithdrawDate AS VARCHAR(10)), '')
), CONCAT(', ',(CHR(10))) ORDER BY COALESCE(ssd.ExitWithdrawDate,'2200-01-01') DESC)
FROM edfi.StudentSchoolAssociation ssd
INNER JOIN edfi.Student st
ON st.StudentUSI = ssd.StudentUSI
INNER JOIN analytics.SchoolDim school
ON school.SchoolKey = CAST(ssd.SchoolId AS VARCHAR)
WHERE st.StudentUniqueId = studentSchoolDim.StudentKey
) AS EnrollmentHistory
,(
SELECT MAX(MaxLastModifiedDate)
FROM (
VALUES (studentSchoolDim.LastModifiedDate)
) AS VALUE(MaxLastModifiedDate)
) AS LastModifiedDate
FROM analytics.StudentSchoolDim studentSchoolDim
LEFT JOIN GradeList gradeFact
ON gradeFact.StudentSchoolKey = studentSchoolDim.StudentSchoolKey
LEFT OUTER JOIN AttendanceHist ah
ON ah.StudentSchoolKey = studentSchoolDim.StudentSchoolKey;
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
-- SPDX-License-Identifier: Apache-2.0
-- Licensed to the Ed-Fi Alliance under one or more agreements.
-- The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
-- See the LICENSE and NOTICES files in the project root for more information.
IF EXISTS (
SELECT 1
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_SCHEMA = 'analytics'
AND ROUTINE_NAME = 'fn_GetStudentGradesSummary'
)
BEGIN
DROP FUNCTION analytics.fn_GetStudentGradesSummary
END
GO

CREATE FUNCTION analytics.fn_GetStudentGradesSummary (
@StudentKey VARCHAR(8000)
,@SchoolKey VARCHAR(8000)
)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @val VARCHAR(8000)

SELECT @val = COALESCE(@VAL, '') + (
CAST(CONCAT (
', '
,CHAR(10)
,course.CourseTitle
,': '
,gradeFact.NumericGradeEarned
) AS VARCHAR(8000))
)
FROM edfi.Student
INNER JOIN edfi.Grade gradeFact
ON gradeFact.StudentUSI = Student.StudentUSI
INNER JOIN edfi.CourseOffering
ON CourseOffering.SchoolId = gradeFact.SchoolId
AND CourseOffering.LocalCourseCode = gradeFact.LocalCourseCode
AND CourseOffering.SchoolYear = gradeFact.SchoolYear
AND CourseOffering.SessionName = gradeFact.SessionName
INNER JOIN edfi.Course
ON Course.CourseCode = CourseOffering.CourseCode
AND Course.EducationOrganizationId = CourseOffering.EducationOrganizationId
INNER JOIN edfi.Descriptor
ON GradeTypeDescriptorId = Descriptor.DescriptorId
INNER JOIN edfi.GradingPeriod
ON gradeFact.GradingPeriodDescriptorId = GradingPeriod.GradingPeriodDescriptorId
AND gradeFact.GradingPeriodSequence = GradingPeriod.PeriodSequence
AND gradeFact.SchoolId = GradingPeriod.SchoolId
AND gradeFact.GradingPeriodSchoolYear = GradingPeriod.SchoolYear
INNER JOIN analytics_config.DescriptorMap
ON DescriptorMap.DescriptorId = GradeTypeDescriptorId
INNER JOIN analytics_config.DescriptorConstant
ON DescriptorConstant.DescriptorConstantId = DescriptorMap.DescriptorConstantId
WHERE DescriptorConstant.ConstantName IN ('GradeType.Semester')
AND Student.StudentUniqueId = @StudentKey
AND gradeFact.SchoolId = @SchoolKey
ORDER BY gradeFact.GradingPeriodDescriptorId DESC
,gradeFact.SchoolId DESC
,GradingPeriod.BeginDate DESC;

RETURN COALESCE(STUFF(COALESCE(@val, ''), 1, 3, ''),'');
END
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
-- SPDX-License-Identifier: Apache-2.0
-- Licensed to the Ed-Fi Alliance under one or more agreements.
-- The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
-- See the LICENSE and NOTICES files in the project root for more information.
DROP VIEW IF EXISTS analytics.equity_StudentHistoryDim;

CREATE
OR REPLACE VIEW analytics.equity_StudentHistoryDim AS
WITH AttendanceHist AS (
SELECT StudentSchoolKey
,COUNT(1) AS DaysEnrolled
,SUM(ReportedAsAbsentFromHomeRoom) AS DaysAbsent
FROM analytics.chrab_ChronicAbsenteeismAttendanceFact
GROUP BY StudentSchoolKey
)
,GradeList AS (
SELECT CONCAT (
studentSectionDim.StudentKey
,'-'
,studentSectionDim.SchoolKey
) AS StudentSchoolKey
,studentSectionDim.LastModifiedDate
,STRING_AGG(CAST(CONCAT (
studentSectionDim.CourseTitle
,': '
,gradeFact.NumericGradeEarned
) AS VARCHAR(8000)), CONCAT(', ','')
ORDER BY gradeFact.GradingPeriodKey DESC) AS GradeSummary
FROM analytics.ews_StudentSectionGradeFact gradeFact
INNER JOIN analytics.StudentSectionDim studentSectionDim
ON gradeFact.StudentSectionKey = studentSectionDim.StudentSectionKey
AND gradeFact.SectionKey = studentSectionDim.SectionKey
INNER JOIN edfi.descriptor
ON descriptor.codevalue = gradeFact.GradeType
INNER JOIN analytics_config.DescriptorMap
ON DescriptorMap.DescriptorId = descriptor.DescriptorId
INNER JOIN analytics_config.DescriptorConstant
ON DescriptorConstant.DescriptorConstantId = DescriptorMap.DescriptorConstantId
WHERE DescriptorConstant.ConstantName IN ('GradeType.Semester')
GROUP BY studentSectionDim.StudentKey
,studentSectionDim.SchoolKey
,studentSectionDim.LastModifiedDate
)

SELECT DISTINCT studentSchoolDim.StudentKey
,studentSchoolDim.StudentSchoolKey
,COALESCE(gradeFact.GradeSummary,'') AS GradeSummary
,studentSchoolDim.SchoolKey AS CurrentSchoolKey
,COALESCE((CAST((DaysEnrolled - DaysAbsent) AS DECIMAL) / CAST(DaysEnrolled AS DECIMAL) * 100), 100.00) AS AttendanceRate
,(
SELECT COUNT(1)
FROM analytics.equity_StudentDisciplineActionDim discipline
WHERE discipline.StudentSchoolKey = studentSchoolDim.StudentSchoolKey
) AS ReferralsAndSuspensions
,(
SELECT STRING_AGG(CONCAT (
SchoolName
,CASE WHEN(ssd.ExitWithdrawDate is not null) then ' ' else '' END
,COALESCE(CAST(ssd.ExitWithdrawDate AS VARCHAR(10)), '')
), CONCAT(', ',(CHR(10))) ORDER BY COALESCE(ssd.ExitWithdrawDate,'2200-01-01') DESC)
FROM edfi.StudentSchoolAssociation ssd
INNER JOIN edfi.Student st
ON st.StudentUSI = ssd.StudentUSI
INNER JOIN analytics.SchoolDim school
ON school.SchoolKey = CAST(ssd.SchoolId AS VARCHAR)
WHERE st.StudentUniqueId = studentSchoolDim.StudentKey
) AS EnrollmentHistory
,(
SELECT MAX(MaxLastModifiedDate)
FROM (
VALUES (studentSchoolDim.LastModifiedDate)
) AS VALUE(MaxLastModifiedDate)
) AS LastModifiedDate
FROM analytics.StudentSchoolDim studentSchoolDim
LEFT JOIN GradeList gradeFact
ON gradeFact.StudentSchoolKey = studentSchoolDim.StudentSchoolKey
LEFT OUTER JOIN AttendanceHist ah
ON ah.StudentSchoolKey = studentSchoolDim.StudentSchoolKey;

0 comments on commit c659826

Please sign in to comment.