I am trying to form a PostgreSQL statement that returns a customer email based on the email type with a given priority. Below I have a table with customers 1 and two. Customer 1 has both personal and company emails whereas customer 2 has on the company.
The problem I am trying to solve is returned the customers personal email if it exists first and if not return the company. So, the personal email is given priority over the company. Is this even possible in PostgreSQL?
customers
+------------+
| cusomterID |
+------------+
| 1 |
| 2 |
+------------+
customer_email
+------------+-------------+
| cusomterID | email_type |
+------------+-------------+
| 1 | personal | -- 0
| 2 | company | -- 1
| 1 | company | -- 1
+------------+-------------+
What I am trying now is not really working. It returns all of the rows and does not filter
SELECT *
FROM customers cs
JOIN cumstomer_email cm ON cm.customerId = cs.customreId
WHERE COALESCE(cm.email_type,0) IN (0,1)