79811743

Date: 2025-11-06 20:31:08
Score: 1
Natty:
Report link

After much help from @JonasMetzler and @DaleK, the following query worked for Snowflake.

My goal was to get all contacts to display on one row instead of multiple rows for each person_key.

SELECT
    p.PERSON_KEY AS "KEY",
    p.PERSON_NAME AS "Person Name",
    MAX(CASE WHEN c.CONTACT_PRIORITY_ORDER = 1 THEN c.CONTACT_FIRST_NAME END) AS "First Contact First Name",
    MAX(CASE WHEN c.CONTACT_PRIORITY_ORDER = 2 THEN c.CONTACT_FIRST_NAME END) AS "Second Contact First Name",
    MAX(CASE WHEN c.CONTACT_PRIORITY_ORDER = 3 THEN c.CONTACT_FIRST_NAME END) AS "Third Contact First Name"
FROM DTBL_PERSON p
LEFT JOIN MTBL_CONTACTS c
       ON p.PERSON_KEY = c.PERSON_KEY
GROUP BY p.PERSON_KEY, p.PERSON_NAME
ORDER BY p.PERSON_KEY;

Because there are multiple contacts attached to each key, I added the contact priority order to identify each which of the contacts should be selected. Then the max function looks at the row and selects the non null value for each column. The group by then identifies how each selection is grouped and delivers the desired single row for each piece of information.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @JonasMetzler
  • User mentioned (0): @DaleK
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Rachel Helm