Advertisement
syntax53

EZRouting PowerQuery

Dec 19th, 2024 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 6.56 KB | Source Code | 0 0
  1. SELECT
  2.     stu.student_number,
  3.     trim(stu.last_name) last_name,
  4.     CASE
  5.         WHEN scf.PSCORE_LEGAL_FIRST_NAME IS NULL THEN trim(stu.first_name)
  6.         ELSE trim(scf.PSCORE_LEGAL_FIRST_NAME)
  7.     END first_name,
  8.     trim(stu.middle_name) middle_name,
  9.     CASE
  10.         WHEN stu.FedEthnicity = 1 THEN 'H'
  11.         WHEN (SELECT COUNT(0) FROM studentrace WHERE studentrace.studentid = stu.id) > 1 THEN 'M'
  12.         ELSE (SELECT RaceCD FROM studentrace WHERE studentrace.studentid = stu.id)
  13.     END AS Race,
  14.     stu.gender,
  15.     to_char(stu.dob, 'MM/DD/YYYY') AS dob,
  16.     CASE
  17.         WHEN to_char(stu.schoolid) = '3' THEN nvl(ps.School_Code, 'PRIVATE_UNKNOWN')
  18.         WHEN to_char(stu.schoolid) = '3375' THEN 'SMS'
  19.         WHEN to_char(stu.schoolid) = '3376' THEN 'STHS'
  20.         WHEN to_char(stu.schoolid) = '3370' THEN 'ERD'
  21.         WHEN to_char(stu.schoolid) = '3368' THEN 'ENF'
  22.         ELSE to_char(stu.schoolid)
  23.     END schoolid,
  24.     CASE
  25.         WHEN stu.grade_level > 0 THEN to_char(stu.grade_level)
  26.         WHEN stu.grade_level = 0 THEN 'KG'
  27.         WHEN stu.grade_level < 0 THEN 'PK'
  28.         ELSE 'N/A'
  29.     END grade_level,
  30.     stu.street,
  31.     stu.city,
  32.     stu.state,
  33.     stu.zip,
  34.     stu.mailing_street,
  35.     stu.mailing_city,
  36.     stu.mailing_state,
  37.     stu.mailing_zip,
  38.     contacts.contact1_name,
  39.     contacts.contact1_relation,
  40.     contacts.contact1_phone1,
  41.     contacts.contact1_phonetype1,
  42.     contacts.contact1_phone2,
  43.     contacts.contact1_phonetype2,
  44.     contacts.contact1_phone3,
  45.     contacts.contact1_phonetype3,
  46.     contacts.contact1_email,
  47.     contacts.contact2_name,
  48.     contacts.contact2_relation,
  49.     contacts.contact2_phone1,
  50.     contacts.contact2_phonetype1,
  51.     contacts.contact2_phone2,
  52.     contacts.contact2_phonetype2,
  53.     contacts.contact2_phone3,
  54.     contacts.contact2_phonetype3,
  55.     contacts.contact2_email,
  56.     contacts.contact3_name,
  57.     contacts.contact3_relation,
  58.     contacts.contact3_phone1,
  59.     contacts.contact3_phonetype1,
  60.     contacts.contact3_phone2,
  61.     contacts.contact3_phonetype2,
  62.     contacts.contact3_phone3,
  63.     contacts.contact3_phonetype3,
  64.     contacts.contact3_email,
  65.     contacts.contact4_name,
  66.     contacts.contact4_relation,
  67.     contacts.contact4_phone1,
  68.     contacts.contact4_phonetype1,
  69.     contacts.contact4_phone2,
  70.     contacts.contact4_phonetype2,
  71.     contacts.contact4_phone3,
  72.     contacts.contact4_phonetype3,
  73.     contacts.contact4_email,
  74.     to_char(stu.EntryDate, 'MM/DD/YYYY') AS EntryDate,
  75.     CASE
  76.         WHEN stu.ExitDate < t_DistrictYr.LastDay THEN to_char(stu.ExitDate, 'MM/DD/YYYY')
  77.         ELSE ''
  78.     END ExitDate,
  79.     CASE
  80.         WHEN sped.IS_SPED_Student_YN = '1' THEN 'Y'
  81.         ELSE 'N'
  82.     END sped,
  83.     CASE
  84.         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)
  85.         ELSE ''
  86.     END nickname
  87. FROM
  88.     students stu
  89. LEFT JOIN
  90.     StudentCoreFields scf ON scf.studentsdcid = stu.dcid
  91. LEFT JOIN
  92.     S_PA_STU_X sped ON sped.studentsdcid = stu.dcid
  93. LEFT JOIN
  94.     U_Private_School ps ON ps.studentsdcid = stu.dcid
  95. LEFT JOIN
  96.     Terms t_DistrictYr ON t_DistrictYr.SchoolId = 0 AND t_DistrictYr.FirstDay <= SYSDATE AND t_DistrictYr.LastDay >= SYSDATE AND t_DistrictYr.IsyearRec = 1
  97. LEFT JOIN
  98.     Terms t_SchoolYr ON t_SchoolYr.SchoolId = stu.SchoolId AND t_SchoolYr.YearId = t_DistrictYr.YearId AND t_SchoolYr.IsyearRec = 1
  99. LEFT OUTER JOIN (
  100.     SELECT
  101.         *
  102.     FROM
  103.         (
  104.             SELECT
  105.                 sca.studentdcid,
  106.                 row_number() over (
  107.                     PARTITION BY sca.studentdcid
  108.                     ORDER BY
  109.                         CASE
  110.                             WHEN scd.iscustodial = 1 OR scd.liveswithflg = 1 THEN 1
  111.                             ELSE 2
  112.                         END,
  113.                         sca.contactpriorityorder
  114.                 ) R,
  115.                 nvl(
  116.                     trim(
  117.                         pers.firstname || ' ' || pers.lastname
  118.                     ),
  119.                     chr(91) || 'NO NAME' || chr(93)
  120.                 ) name,
  121.                 CASE
  122.                     WHEN (scd.iscustodial = 1 OR scd.liveswithflg = 1) THEN rel.code
  123.                     ELSE 'EC-' || rel.code
  124.                 END relation,
  125.                 personphonenumber.phone1,
  126.                 personphonenumber.phone2,
  127.                 personphonenumber.phone3,
  128.                 personphonetype.phonetype1,
  129.                 personphonetype.phonetype2,
  130.                 personphonetype.phonetype3,
  131.                 CASE
  132.                     WHEN (scd.iscustodial = 1 OR scd.liveswithflg = 1) THEN personemail.email
  133.                     ELSE ''
  134.                 END email
  135.             FROM
  136.                 studentcontactassoc sca
  137.                 INNER JOIN studentcontactdetail scd ON sca.studentcontactassocid = scd.studentcontactassocid
  138.                     AND nvl(scd.startdate, SYSDATE) <= SYSDATE
  139.                     AND nvl(scd.enddate, SYSDATE) >= SYSDATE
  140.                     AND (scd.iscustodial = 1 OR scd.liveswithflg = 1 OR scd.isemergency = 1)
  141.                 LEFT OUTER JOIN codeset rel ON sca.currreltypecodesetid = rel.codesetid
  142.                 LEFT OUTER JOIN person pers ON sca.personid = pers.id
  143.                 LEFT OUTER JOIN (
  144.                     SELECT
  145.                         *
  146.                     FROM
  147.                         (
  148.                             SELECT
  149.                                 personphonenumberassoc1.personid,
  150.                                 personphonenumberassoc1.phonenumberasentered phonenumber,
  151.                                 row_number() over (
  152.                                     PARTITION BY personphonenumberassoc1.personid
  153.                                     ORDER BY personphonenumberassoc1.phonenumberpriorityorder
  154.                                 ) R
  155.                             FROM
  156.                                 personphonenumberassoc personphonenumberassoc1
  157.                         ) pivot (
  158.                             MAX(phonenumber) FOR R IN (1 phone1, 2 phone2, 3 phone3)
  159.                         )
  160.                 ) personphonenumber ON pers.id = personphonenumber.personid
  161.                 LEFT OUTER JOIN (
  162.                     SELECT
  163.                         *
  164.                     FROM
  165.                         (
  166.                             SELECT
  167.                                 personphonenumberassoc2.personid,
  168.                                 phonetype.code phonetype,
  169.                                 row_number() over (
  170.                                     PARTITION BY personphonenumberassoc2.personid
  171.                                     ORDER BY personphonenumberassoc2.phonenumberpriorityorder
  172.                                 ) R
  173.                             FROM
  174.                                 personphonenumberassoc personphonenumberassoc2
  175.                                 LEFT OUTER JOIN codeset phonetype ON personphonenumberassoc2.phonetypecodesetid = phonetype.codesetid
  176.                         ) pivot (
  177.                             MAX(phonetype) FOR R IN (1 phonetype1, 2 phonetype2, 3 phonetype3)
  178.                         )
  179.                 ) personphonetype ON pers.id = personphonetype.personid
  180.                 LEFT OUTER JOIN (
  181.                     SELECT
  182.                         *
  183.                     FROM
  184.                         (
  185.                             SELECT
  186.                                 pea.personid,
  187.                                 ea.EmailAddress email,
  188.                                 row_number() over (
  189.                                     partition BY pea.personid
  190.                                     order by pea.EmailAddressPriorityOrder
  191.                                 ) R
  192.                             FROM
  193.                                 PersonEmailAddressAssoc pea
  194.                             RIGHT JOIN
  195.                                 EMAILADDRESS ea ON ea.EMAILADDRESSID = pea.EMAILADDRESSID
  196.                         ) pivot (
  197.                             MAX(email) FOR R IN (1 email)
  198.                         )
  199.                 ) personemail ON pers.id = personemail.personid
  200.         ) pivot (
  201.             MAX(name) name,
  202.             MAX(relation) relation,
  203.             MAX(phone1) phone1,
  204.             MAX(phone2) phone2,
  205.             MAX(phone3) phone3,
  206.             MAX(phonetype1) phonetype1,
  207.             MAX(phonetype2) phonetype2,
  208.             MAX(phonetype3) phonetype3,
  209.             MAX(email) email FOR R IN (1 contact1, 2 contact2, 3 contact3, 4 contact4)
  210.         )
  211. ) contacts ON stu.dcid = contacts.studentdcid
  212. WHERE
  213.     (stu.enroll_status = 0 OR stu.enroll_status = 2) AND stu.ExitDate > t_DistrictYr.FirstDay
  214. ORDER BY
  215.     stu.lastfirst
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement