Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SELECT
- stu.student_number,
- trim(stu.last_name) last_name,
- CASE
- WHEN scf.PSCORE_LEGAL_FIRST_NAME IS NULL THEN trim(stu.first_name)
- ELSE trim(scf.PSCORE_LEGAL_FIRST_NAME)
- END first_name,
- trim(stu.middle_name) middle_name,
- CASE
- WHEN stu.FedEthnicity = 1 THEN 'H'
- WHEN (SELECT COUNT(0) FROM studentrace WHERE studentrace.studentid = stu.id) > 1 THEN 'M'
- ELSE (SELECT RaceCD FROM studentrace WHERE studentrace.studentid = stu.id)
- END AS Race,
- stu.gender,
- to_char(stu.dob, 'MM/DD/YYYY') AS dob,
- CASE
- WHEN to_char(stu.schoolid) = '3' THEN nvl(ps.School_Code, 'PRIVATE_UNKNOWN')
- WHEN to_char(stu.schoolid) = '3375' THEN 'SMS'
- WHEN to_char(stu.schoolid) = '3376' THEN 'STHS'
- WHEN to_char(stu.schoolid) = '3370' THEN 'ERD'
- WHEN to_char(stu.schoolid) = '3368' THEN 'ENF'
- ELSE to_char(stu.schoolid)
- END schoolid,
- CASE
- WHEN stu.grade_level > 0 THEN to_char(stu.grade_level)
- WHEN stu.grade_level = 0 THEN 'KG'
- WHEN stu.grade_level < 0 THEN 'PK'
- ELSE 'N/A'
- END grade_level,
- stu.street,
- stu.city,
- stu.state,
- stu.zip,
- stu.mailing_street,
- stu.mailing_city,
- stu.mailing_state,
- stu.mailing_zip,
- contacts.contact1_name,
- contacts.contact1_relation,
- contacts.contact1_phone1,
- contacts.contact1_phonetype1,
- contacts.contact1_phone2,
- contacts.contact1_phonetype2,
- contacts.contact1_phone3,
- contacts.contact1_phonetype3,
- contacts.contact1_email,
- contacts.contact2_name,
- contacts.contact2_relation,
- contacts.contact2_phone1,
- contacts.contact2_phonetype1,
- contacts.contact2_phone2,
- contacts.contact2_phonetype2,
- contacts.contact2_phone3,
- contacts.contact2_phonetype3,
- contacts.contact2_email,
- contacts.contact3_name,
- contacts.contact3_relation,
- contacts.contact3_phone1,
- contacts.contact3_phonetype1,
- contacts.contact3_phone2,
- contacts.contact3_phonetype2,
- contacts.contact3_phone3,
- contacts.contact3_phonetype3,
- contacts.contact3_email,
- contacts.contact4_name,
- contacts.contact4_relation,
- contacts.contact4_phone1,
- contacts.contact4_phonetype1,
- contacts.contact4_phone2,
- contacts.contact4_phonetype2,
- contacts.contact4_phone3,
- contacts.contact4_phonetype3,
- contacts.contact4_email,
- to_char(stu.EntryDate, 'MM/DD/YYYY') AS EntryDate,
- CASE
- WHEN stu.ExitDate < t_DistrictYr.LastDay THEN to_char(stu.ExitDate, 'MM/DD/YYYY')
- ELSE ''
- END ExitDate,
- CASE
- WHEN sped.IS_SPED_Student_YN = '1' THEN 'Y'
- ELSE 'N'
- END sped,
- CASE
- WHEN scf.PSCORE_LEGAL_FIRST_NAME IS NOT NULL AND trim(stu.first_name) != trim(scf.PSCORE_LEGAL_FIRST_NAME) THEN trim(stu.first_name)
- ELSE ''
- END nickname
- FROM
- students stu
- LEFT JOIN
- StudentCoreFields scf ON scf.studentsdcid = stu.dcid
- LEFT JOIN
- S_PA_STU_X sped ON sped.studentsdcid = stu.dcid
- LEFT JOIN
- U_Private_School ps ON ps.studentsdcid = stu.dcid
- LEFT JOIN
- Terms t_DistrictYr ON t_DistrictYr.SchoolId = 0 AND t_DistrictYr.FirstDay <= SYSDATE AND t_DistrictYr.LastDay >= SYSDATE AND t_DistrictYr.IsyearRec = 1
- LEFT JOIN
- Terms t_SchoolYr ON t_SchoolYr.SchoolId = stu.SchoolId AND t_SchoolYr.YearId = t_DistrictYr.YearId AND t_SchoolYr.IsyearRec = 1
- LEFT OUTER JOIN (
- SELECT
- *
- FROM
- (
- SELECT
- sca.studentdcid,
- row_number() over (
- PARTITION BY sca.studentdcid
- ORDER BY
- CASE
- WHEN scd.iscustodial = 1 OR scd.liveswithflg = 1 THEN 1
- ELSE 2
- END,
- sca.contactpriorityorder
- ) R,
- nvl(
- trim(
- pers.firstname || ' ' || pers.lastname
- ),
- chr(91) || 'NO NAME' || chr(93)
- ) name,
- CASE
- WHEN (scd.iscustodial = 1 OR scd.liveswithflg = 1) THEN rel.code
- ELSE 'EC-' || rel.code
- END relation,
- personphonenumber.phone1,
- personphonenumber.phone2,
- personphonenumber.phone3,
- personphonetype.phonetype1,
- personphonetype.phonetype2,
- personphonetype.phonetype3,
- CASE
- WHEN (scd.iscustodial = 1 OR scd.liveswithflg = 1) THEN personemail.email
- ELSE ''
- END email
- FROM
- studentcontactassoc sca
- INNER JOIN studentcontactdetail scd ON sca.studentcontactassocid = scd.studentcontactassocid
- AND nvl(scd.startdate, SYSDATE) <= SYSDATE
- AND nvl(scd.enddate, SYSDATE) >= SYSDATE
- AND (scd.iscustodial = 1 OR scd.liveswithflg = 1 OR scd.isemergency = 1)
- LEFT OUTER JOIN codeset rel ON sca.currreltypecodesetid = rel.codesetid
- LEFT OUTER JOIN person pers ON sca.personid = pers.id
- LEFT OUTER JOIN (
- SELECT
- *
- FROM
- (
- SELECT
- personphonenumberassoc1.personid,
- personphonenumberassoc1.phonenumberasentered phonenumber,
- row_number() over (
- PARTITION BY personphonenumberassoc1.personid
- ORDER BY personphonenumberassoc1.phonenumberpriorityorder
- ) R
- FROM
- personphonenumberassoc personphonenumberassoc1
- ) pivot (
- MAX(phonenumber) FOR R IN (1 phone1, 2 phone2, 3 phone3)
- )
- ) personphonenumber ON pers.id = personphonenumber.personid
- LEFT OUTER JOIN (
- SELECT
- *
- FROM
- (
- SELECT
- personphonenumberassoc2.personid,
- phonetype.code phonetype,
- row_number() over (
- PARTITION BY personphonenumberassoc2.personid
- ORDER BY personphonenumberassoc2.phonenumberpriorityorder
- ) R
- FROM
- personphonenumberassoc personphonenumberassoc2
- LEFT OUTER JOIN codeset phonetype ON personphonenumberassoc2.phonetypecodesetid = phonetype.codesetid
- ) pivot (
- MAX(phonetype) FOR R IN (1 phonetype1, 2 phonetype2, 3 phonetype3)
- )
- ) personphonetype ON pers.id = personphonetype.personid
- LEFT OUTER JOIN (
- SELECT
- *
- FROM
- (
- SELECT
- pea.personid,
- ea.EmailAddress email,
- row_number() over (
- partition BY pea.personid
- order by pea.EmailAddressPriorityOrder
- ) R
- FROM
- PersonEmailAddressAssoc pea
- RIGHT JOIN
- EMAILADDRESS ea ON ea.EMAILADDRESSID = pea.EMAILADDRESSID
- ) pivot (
- MAX(email) FOR R IN (1 email)
- )
- ) personemail ON pers.id = personemail.personid
- ) pivot (
- MAX(name) name,
- MAX(relation) relation,
- MAX(phone1) phone1,
- MAX(phone2) phone2,
- MAX(phone3) phone3,
- MAX(phonetype1) phonetype1,
- MAX(phonetype2) phonetype2,
- MAX(phonetype3) phonetype3,
- MAX(email) email FOR R IN (1 contact1, 2 contact2, 3 contact3, 4 contact4)
- )
- ) contacts ON stu.dcid = contacts.studentdcid
- WHERE
- (stu.enroll_status = 0 OR stu.enroll_status = 2) AND stu.ExitDate > t_DistrictYr.FirstDay
- ORDER BY
- stu.lastfirst
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement