I have a HUGE table that contains details about all customers, such as their phone numbers and email addresses. The table can have multiple rows for each customer, indicating that at some point they have changed their phone number, email address, or other profile information. Every row has a day column, indicating the day that the profile change happened (i.e. the state of their profile on that day).
I want to extract every phone number and email address that each customer has ever had on their profile, and also add a boolean flag (called 'live') indicating which phone number and email is currently associated with their profile (i.e. the most recent record we have in the table for that customer).
Here is my current query:
SELECT DISTINCT
customer_id,
phone_number,
email AS email_address,
CASE
WHEN day = (
SELECT
MAX(temp.day)
FROM
customer AS temp
WHERE
customer.customer_id = temp.customer_id
)
THEN true
ELSE false
END AS live
FROM
customer
I assume this is terribly inefficient given that there are billions of rows for millions of different customers. How can I impove this query to achieve what I want (maybe a window function?), or would soem completely different way better achieve what I want?