我正在尝试重新创建学生信息系统(SIS)的级联逻辑,以构建一份跟踪报告,将所有毕业要求汇总到一个地方。
目前课程可以有 1 到 5 个与之相关的科目,每个科目都有一个上限或需要满足的最低要求,如果满足总数,则输入该科目的学分将级联到科目 2、科目 3 等。如果所有科目“桶”都已满,最后进入选修课。
这对于 SQL 来说可能吗?因为我更喜欢在 SQL 级别运行它?
我曾尝试设置递归 CTE 来计算此值,但我没有找到解决方案。任何帮助或建议将不胜感激。
示例表:
信用要求表
主题 | 最大信用 |
---|---|
艺术 | 1.0 |
职业与技术 | 1.0 |
公民 | 0.5 |
继续世界问题 | 0.5 |
英语 | 4.0 |
健康 | 0.5 |
数学 | 3.0 |
物理教育 | 1.5 |
科学 | 1.0 |
美国历史 | 1.0 |
选修课 | 7.0 |
学生课程历史样本
CREATE TABLE StudentCourses (
stuID INT,
crsID VARCHAR(10),
crsTitle VARCHAR(50),
creditEarned DECIMAL(3,3),
subject1 VARCHAR(50),
subject2 VARCHAR(50),
subject3 VARCHAR(50),
subject4 VARCHAR(50),
subject5 VARCHAR(50)
);
INSERT INTO StudentCourses (stuID, crsID, crsTitle, creditEarned, subject1, subject2, subject3, subject4, subject5)
VALUES
(12345, 'TRMSC', 'SPANISH I', 0.500, 'MISCELLANEOUS', NULL, NULL, NULL, NULL),
(12345, '7732', 'CHEMISTRY', 0.500, 'SCIENCE', NULL, NULL, NULL, NULL),
(12345, '2371', 'AP ENG LIT/COMP', 0.500, 'ENGLISH', NULL, NULL, NULL, NULL),
(12345, '3611', 'PAP INT MTH III', 0.500, 'MATHEMATICS', NULL, NULL, NULL, NULL),
(12345, '8031', 'PAP WA ST HIST', 0.500, 'WASH ST HISTORY', NULL, NULL, NULL, NULL),
(12345, '7772', 'PHYSICS', 0.500, 'SCIENCE', NULL, NULL, NULL, NULL),
(12345, '2242', 'PAP ENGLISH 10', 0.500, 'ENGLISH', NULL, NULL, NULL, NULL),
(12345, '8242', 'AP US HISTORY', 0.500, 'U.S. HISTORY', NULL, NULL, NULL, NULL),
(12345, '0262', 'POTTERY I', 0.500, 'ART', NULL, NULL, NULL, NULL),
(12345, '2372', 'AP ENG LIT/COMP', 0.500, 'ENGLISH', NULL, NULL, NULL, NULL),
(12345, '1521', 'SPANISH 2', 0.500, 'MISCELLANEOUS', NULL, NULL, NULL, NULL),
(12345, '7641', 'ZOOLOGY', 0.500, 'SCIENCE', NULL, NULL, NULL, NULL),
(12345, '4281', 'BUSINESS LAW', 0.500, 'CAREER & TECH', NULL, NULL, NULL, NULL),
(12345, '1522', 'SPANISH 2', 0.500, 'MISCELLANEOUS', NULL, NULL, NULL, NULL),
(12345, '4291', 'AD BUSINESS LAW', 0.500, 'CAREER & TECH', 'CAREER & TECH', NULL, NULL, NULL),
(12345, '9872', 'STUD GOVT', 0.500, 'MISCELLANEOUS', NULL, NULL, NULL, NULL),
(12345, '7642', 'ZOOLOGY', 0.500, 'SCIENCE', NULL, NULL, NULL, NULL),
(12345, '9871', 'STUD GOVT', 0.500, 'MISCELLANEOUS', NULL, NULL, NULL, NULL),
(12345, 'TRMTH', 'ALGEBRA', 1.000, 'MATHEMATICS', NULL, NULL, NULL, NULL),
(12345, '2171', 'PAP ENGLISH 9', 0.500, 'ENGLISH', NULL, NULL, NULL, NULL),
(12345, '3821', 'AP CALCULUS', 0.500, 'MATHEMATICS', NULL, NULL, NULL, NULL),
(12345, '4201', 'TECH TOOL FYF', 0.500, 'CAREER & TECH', NULL, NULL, NULL, NULL),
(12345, '0261', 'POTTERY I', 0.500, 'ART', NULL, NULL, NULL, NULL),
(12345, '6252V', 'HEALTH WELLNESS', 0.500, 'HEALTH', NULL, NULL, NULL, NULL),
(12345, '9603H', 'HSPE SCIENCE', 0.250, 'MISCELLANEOUS', NULL, NULL, NULL, NULL),
(12345, '6291', 'PSYCH/HLTH ISS', 0.500, 'CAREER & TECH', NULL, NULL, NULL, NULL),
(12345, '2592', 'SR COMP/CON CUL', 0.500, 'ENGLISH', NULL, NULL, NULL, NULL),
(12345, '7771', 'PHYSICS', 0.500, 'SCIENCE', NULL, NULL, NULL, NULL),
(12345, '2591', 'SR COMP/CON CUL', 0.500, 'ENGLISH', 'CAREER & TECH', NULL, NULL, NULL),
(12345, '9871', 'STUD GOVT', 0.500, 'MISCELLANEOUS', NULL, NULL, NULL, NULL),
(12345, '3822', 'AP CALCULUS', 0.500, 'MATHEMATICS', NULL, NULL, NULL, NULL),
(12345, '9871', 'STUD GOVT', 0.500, 'MISCELLANEOUS', NULL, NULL, NULL, NULL),
(12345, '7731', 'CHEMISTRY', 0.500, 'SCIENCE', 'CAREER & TECH', NULL, NULL, NULL),
(12345, '8072', 'PAP WORLD STUDY', 0.500, 'CONT WRLD PROB', NULL, NULL, NULL, NULL),
(12345, '9872', 'STUD GOVT', 0.500, 'MISCELLANEOUS', NULL, NULL, NULL, NULL),
(12345, '6202', 'AEROBIC FITNESS', 0.500, 'PHYSICAL ED', NULL, NULL, NULL, NULL),
(12345, '9601H', 'HSPE READING', 0.250, 'MISCELLANEOUS', NULL, NULL, NULL, NULL),
(12345, '3602', 'INTEG MATH III', 0.500, 'MATHEMATICS', NULL, NULL, NULL, NULL),
(12345, 'TRMSC', 'SPANISH I', 0.500, 'MISCELLANEOUS', NULL, NULL, NULL, NULL),
(12345, '3831', 'CALCULUS II', 0.500, 'MATHEMATICS', NULL, NULL, NULL, NULL),
(12345, '3832', 'CALCULUS II', 0.500, 'MATHEMATICS', NULL, NULL, NULL, NULL),
(12345, '8441', 'AP COMP GOV/POL', 0.500, 'CIVICS', NULL, NULL, NULL, NULL),
(12345, '9872', 'STUD GOVT', 0.500, 'MISCELLANEOUS', NULL, NULL, NULL, NULL),
(12345, '6292', 'PSYCH/HLTH ISS', 0.500, 'CAREER & TECH', NULL, NULL, NULL, NULL),
(12345, '9872', 'STUD GOVT', 0.500, 'MISCELLANEOUS', NULL, NULL, NULL, NULL),
(12345, '6201', 'AEROBIC FITNESS', 0.500, 'PHYSICAL ED', NULL, NULL, NULL, NULL),
(12345, '2172', 'PAP ENGLISH 9', 0.500, 'ENGLISH', NULL, NULL, NULL, NULL),
(12345, '9604H', 'HSPE WRITING', 0.250, 'MISCELLANEOUS', NULL, NULL, NULL, NULL),
(12345, 'TRMTH', 'GEOMETRY', 1.000, 'MATHEMATICS', NULL, NULL, NULL, NULL),
(12345, '3701', 'PAP INT MATH IV', 0.500, 'MATHEMATICS', NULL, NULL, NULL, NULL),
(12345, '3702', 'PAP INT MATH IV', 0.500, 'MATHEMATICS', NULL, NULL, NULL, NULL),
(12345, '8241', 'AP US HISTORY', 0.500, 'U.S. HISTORY', NULL, NULL, NULL, NULL),
(12345, '8511', 'CUL PRJ/HS BYND', 0.500, 'SENIOR PROJECT', NULL, NULL, NULL, NULL),
(12345, '9871', 'STUD GOVT', 0.500, 'MISCELLANEOUS', NULL, NULL, NULL, NULL),
(12345, '8442', 'AP COMP GOV/POL', 0.500, 'CONT WRLD PROB', NULL, NULL, NULL, NULL);
预期结果(数学不考虑级联)
主题 | 最大积分 | stu主题 | 已获得学分 |
---|---|---|---|
其他 | 6.750 | ||
高级项目 | 0.500 | ||
WASH ST 历史 | 0.500 | ||
艺术 | 1.0 | 艺术 | 1.000 |
职业与技术 | 1.0 | 职业与技术 | 2.500 |
公民 | 0.5 | 公民 | 0.500 |
继续世界问题 | 0.5 | 继续世界问题 | 1.000 |
英语 | 4.0 | 英语 | 4.000 |
健康 | 0.5 | 健康 | 0.500 |
数学 | 3.0 | 数学 | 6.000 |
物理教育 | 1.5 | 物理教育 | 1.000 |
科学 | 1.0 | 科学 | 3.000 |
美国历史 | 1.0 | 美国历史 | 1.000 |
这是一个简单的加入以获得决赛桌。
检查如下:
WITH SC AS (
select DISTINCT
subject1 as stuSubject,
SUM(creditEarned) OVER (PARTITION BY subject1) AS creditEarned
from StudentCourses
)
SELECT
CR.subject,
CR.maxcredit,
SC.stuSubject,
SC.creditEarned
FROM SC left join CreditReq CR
on SC.stuSubject = CR.subject
ORDER BY CR.subject;
演示代码:db<>fiddle