Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stuck on getting only first valid record from associated table

Status
Not open for further replies.

ghbeers

Programmer
Jul 17, 2014
76
US
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

 
not clear what you are after. Could you please give us a sample of inputs for each table and desired output.

As for being slow "JOIN USER_TERM UT ON STTR.STUDENT_TERMS_ID LIKE '%' + UT.STTR_TERM" will most likely be a killer

Regards

Frederico Fonseca
SysSoft Integrated Ltd

FAQ219-2884
FAQ181-2886
 
I figured this out about 2 seconds before I saw you response come in. Here's what I ended up doing with the last CTE that is working correctly. Thank you for getting back to me.

HAVE_GRADE_STUS AS
(
SELECT distinct
RPT.STTR_KEY
FROM RPT_STTR_STUS RPT
JOIN STUDENT_TERMS_LS STTR ON RPT.STTR_KEY = STTR.STUDENT_TERMS_ID
LEFT JOIN (SELECT STAC.STUDENT_ACAD_CRED_ID AS STAC_KEY,
Row_Number() Over (Partition by STAC.STUDENT_ACAD_CRED_ID
Order by STAC.STC_START_DATE) As RowId
FROM STUDENT_ACAD_CRED STAC
WHERE STAC.STC_VERIFIED_GRADE IS NOT NULL) SEL1
ON SEL1.STAC_KEY = STTR.STTR_STUDENT_ACAD_CRED
AND RowId = 1
)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top