I am writing a data export where I need to return one row from a selection where there may be multiple rows. In this case, the second table is the telephone_current table. This table includes a row for several telephone types (CA, MA, PR, etc.), and they are not in any particular order. If the individual has a CA, I need to include that record; if not, then I would use either type MA or PR.
The query below works, technically, but it will run excruciatingly slow (10 minutes or more).
I need advice to fix this query to get one row (record) per individual. The slowdown occurs when I include the self join telephone_current tc. Note. I've also moved the AND into the WHERE clause, which runs with the time delay.
SELECT distinct igp.isu_id PersonnelNumber
, igp.preferred_first_name FirstName
, igp.current_last_name LastName
, NULL Title
, igp.current_mi MiddleInitial
, pd.email_preferred_address
, tc.phone_number_combined
, igp.isu_username networkID
, '0' GroupID
, e.home_organization_desc GroupName
, CASE
WHEN substr(e.employee_class,1,1) in ( 'N', 'C') THEN 'staff'
WHEN substr(e.employee_class,1,1) = 'F' THEN 'faculty'
ELSE 'other'
END GroupType
FROM isu_general_person igp
JOIN person_detail pd ON igp.person_uid = pd.person_uid
JOIN telephone_current tc ON igp.person_uid = tc.entity_uid
AND tc.phone_number = (
SELECT p.phone_number
FROM telephone_current p
WHERE tc.entity_uid = p.entity_uid
ORDER BY phone_type
FETCH FIRST 1 ROW ONLY
)
LEFT JOIN employee e ON igp.person_uid = e.person_uid
-- LEFT JOIN faculty f ON igp.person_uid = f.person_uid
WHERE 1=1
AND e.employee_status = 'A'
AND substr(e.employee_class,1,1) in ( 'N', 'C', 'F')
AND igp.isu_username IS NOT NULL
;