I am absolutely stuck on finding ONLY 1 record that exists in an associated table that meets my minimum requirement. I have a table STUDENT_TERMS that is for a student and specific term. In the table are keys to all of the course info the student has signed up for the semester. When I go to the course info records (STUDENT_ACAD_CRED), I want to stop after I find the first record where the grade is not NULL. Everything I've tried is not working or it takes way too long to execute. Here is my script so far. The last CTE is the ome I just can't get right, really missing something. Any help will be greatly appreciated.
-- Student with 0 GPA or Completed credits for Term
WITH USER_TERM AS
(
SELECT DISTINCT
T.TERMS_ID AS 'USR_TERM',
TL.TERM_CENSUS_DATES AS 'CUR_CENSUS_DATE',
T.TERMS_ID + '*UG' AS 'STTR_TERM',
T.TERM_END_DATE AS 'TERM_END'
FROM TERMS T
LEFT JOIN TERMS_LS TL ON T.TERMS_ID = TL.TERMS_ID
WHERE TL.TERM_CENSUS_DATES IS NOT NULL
AND TL.POS = 1
AND TL.TERMS_ID = '2017/FA'
-- AND TL.TERMS_ID = UPPER( <<Enter Term YYYY/SS [Text]>> )
)
--SELECT * FROM USER_TERM
,RPT_STTR_STUS AS
(
SELECT DISTINCT TOP 60
SUBSTRING(STTR.STUDENT_TERMS_ID, 1,7) 'STUDENT_ID',
STTR.STUDENT_TERMS_ID 'STTR_KEY',
STTRS.STTR_STATUS 'STTR_STATUS'
FROM STUDENT_TERMS STTR
JOIN USER_TERM UT ON STTR.STUDENT_TERMS_ID LIKE '%' + UT.STTR_TERM
JOIN STTR_STATUSES STTRS ON STTR.STUDENT_TERMS_ID = STTRS.STUDENT_TERMS_ID
AND STTRS.POS = 1
AND STTRS.STTR_STATUS IN ('R','P','T')
)
--SELECT * FROM RPT_STTR_STUS
,HAVE_GRADE_STUS AS
(
SELECT DISTINCT
RSS.STUDENT_ID 'STUDENT_ID'
FROM RPT_STTR_STUS RSS
WHERE RSS.STUDENT_ID =
(
SELECT TOP 1 STAC1.STC_PERSON_ID
FROM STUDENT_ACAD_CRED STAC1
JOIN STUDENT_TERMS_LS STTRL ON STAC1.STUDENT_ACAD_CRED_ID = STTRL.STTR_STUDENT_ACAD_CRED
AND STAC1.STC_VERIFIED_GRADE IS NOT NULL
)
)
SELECT * FROM HAVE_GRADE_STUS
-- Student with 0 GPA or Completed credits for Term
WITH USER_TERM AS
(
SELECT DISTINCT
T.TERMS_ID AS 'USR_TERM',
TL.TERM_CENSUS_DATES AS 'CUR_CENSUS_DATE',
T.TERMS_ID + '*UG' AS 'STTR_TERM',
T.TERM_END_DATE AS 'TERM_END'
FROM TERMS T
LEFT JOIN TERMS_LS TL ON T.TERMS_ID = TL.TERMS_ID
WHERE TL.TERM_CENSUS_DATES IS NOT NULL
AND TL.POS = 1
AND TL.TERMS_ID = '2017/FA'
-- AND TL.TERMS_ID = UPPER( <<Enter Term YYYY/SS [Text]>> )
)
--SELECT * FROM USER_TERM
,RPT_STTR_STUS AS
(
SELECT DISTINCT TOP 60
SUBSTRING(STTR.STUDENT_TERMS_ID, 1,7) 'STUDENT_ID',
STTR.STUDENT_TERMS_ID 'STTR_KEY',
STTRS.STTR_STATUS 'STTR_STATUS'
FROM STUDENT_TERMS STTR
JOIN USER_TERM UT ON STTR.STUDENT_TERMS_ID LIKE '%' + UT.STTR_TERM
JOIN STTR_STATUSES STTRS ON STTR.STUDENT_TERMS_ID = STTRS.STUDENT_TERMS_ID
AND STTRS.POS = 1
AND STTRS.STTR_STATUS IN ('R','P','T')
)
--SELECT * FROM RPT_STTR_STUS
,HAVE_GRADE_STUS AS
(
SELECT DISTINCT
RSS.STUDENT_ID 'STUDENT_ID'
FROM RPT_STTR_STUS RSS
WHERE RSS.STUDENT_ID =
(
SELECT TOP 1 STAC1.STC_PERSON_ID
FROM STUDENT_ACAD_CRED STAC1
JOIN STUDENT_TERMS_LS STTRL ON STAC1.STUDENT_ACAD_CRED_ID = STTRL.STTR_STUDENT_ACAD_CRED
AND STAC1.STC_VERIFIED_GRADE IS NOT NULL
)
)
SELECT * FROM HAVE_GRADE_STUS