I'm using PowerSchool (Oracle DB), and in order to make an export for our emergency robo-calling service, I need to pull a report of students and the top 7 emergency phone numbers associated to them. I have a query that pulls down just the emergency numbers, but am having trouble pulling the nth phone record into the correct context. When I run the current code, I get "ORA-00923: FROM keyword not found where expected".
I can select all the values from the query in the WITH statement and that runs as expected. However, I don't know how to properly grab them in the main query.
WITH emerglist as (
select emergnumbers.stu_dcid, emergnumbers.contact_priority_order, 1 as phone_order, emergnumbers.contact_phone_1 as phone_number
from emergnumbers where contact_phone_1 is not null
UNION
select emergnumbers.stu_dcid, emergnumbers.contact_priority_order, 2 as phone_order, emergnumbers.contact_phone_2 as phone_number
from emergnumbers where contact_phone_2 is not null
UNION
select emergnumbers.stu_dcid, emergnumbers.contact_priority_order, 3 as phone_order, emergnumbers.contact_phone_3 as phone_number
from emergnumbers where contact_phone_3 is not null
)
--select * from emerglist; -- this works
select distinct
s.student_number as referencecode,
s.first_name as firstname,
s.last_name as lastname,
--emergency numbers
(select enum.phone_number from enum where rn=1) as emerg_1
(select enum.phone_number from enum where rn=2) as emerg_2,
(select enum.phone_number from enum where rn=3) as emerg_3,
(select enum.phone_number from enum where rn=4) as emerg_4,
(select enum.phone_number from enum where rn=5) as emerg_5,
(select enum.phone_number from enum where rn=6) as emerg_6,
(select enum.phone_number from enum where rn=7) as emerg_7
from students s
left outer join ( select emerglist.*, rownum rn from emerglist order by contact_priority_order, phone_order ) enum on s.dcid = enum.stu_dcid
where s.enroll_status=0
;
Edit to add: My goal is to have one row per student, with as many columns of emergency numbers filled in as possible, with no gaps (e.g. can't have emerg1 = #, emerg_2 = null, emerg_3 = #). Each student can have 0-10 contacts, each contact can have 0-5 numbers. We are limited to 7 numbers per student. This is a subset of a larger query that also pulls in primary numbers for normal messages like snow days, email addresses, and other demographics. All that works except the emergency numbers. These get used only when there is a true emergency and we want to reach as many numbers as possible.
Sample data from EmergList:
STU_DCID CONTACT_PRIORITY_ORDER PHONE_ORDER PHONE_NUMBER
52 1 1 (222) 222-6476
52 2 1 (222) 555-9649
52 2 2 (222) 888-2212
58 1 1 (222) 222-1734
58 1 2 (222) 555-7222
Desired results:
REFERENCECODE FIRSTNAME LASTNAME EMERG_1 EMERG_2 EMERG_3 EMERG_4 EMERG_5 EMERG_6 EMERG_7
52 John Smith (222) 222-6476 (222) 555-9649 (222) 888-2212
58 Mary Jones (222) 222-1734 (222) 555-7222