To get the last term attended by each student, you can use a query like the following:
sqlWITH LastTermCTE AS (
SELECT
STTR_STUDENT,
STTR_TERM,
ROW_NUMBER() OVER (PARTITION BY STTR_STUDENT ORDER BY TERM_END_DATE DESC) AS rn
FROM
STUDENT_TERMS_VIEW
)
SELECT
LT.STTR_STUDENT,
LT.STTR_TERM,
T.TERM_END_DATE
FROM
LastTermCTE LT
JOIN TERMS T ON LT.STTR_TERM = T.TERM_ID
WHERE
LT.rn = 1;
This query uses a Common Table Expression (CTE) to rank the terms attended by each student based on the TERM_END_DATE
in descending order. Then, it selects only the rows with rn = 1
, which represents the last term attended by each student. Finally, it joins the result with the TERMS
table to get the TERM_END_DATE
for each term.
Please note that the exact column names may vary based on your actual table schema. Adjust the column names accordingly in the query.
المزيد من المعلومات
لحل المشكلة والحصول على آخر فصل حضره كل طالب، يمكنك استخدام الاستعلام التالي:
sqlWITH LastTermCTE AS (
SELECT
STTR_STUDENT,
STTR_TERM,
ROW_NUMBER() OVER (PARTITION BY STTR_STUDENT ORDER BY TERMS.TERM_END_DATE DESC) AS rn
FROM
STUDENT_TERMS_VIEW
JOIN TERMS ON STUDENT_TERMS_VIEW.STTR_TERM = TERMS.TERM_ID
)
SELECT
STTR_STUDENT,
STTR_TERM,
TERM_END_DATE
FROM
LastTermCTE
WHERE
rn = 1;
في هذا الاستعلام، تم إضافة الجملة JOIN TERMS ON STUDENT_TERMS_VIEW.STTR_TERM = TERMS.TERM_ID
للانضمام إلى جدول TERMS
والحصول على تاريخ نهاية الفصل. استخدمت الجملة PARTITION BY STTR_STUDENT
لتقسيم البيانات حسب كل طالب على حده، و ORDER BY TERMS.TERM_END_DATE DESC
لترتيب الفصول بتاريخ النهاية بترتيب تنازلي.