I would like some help on this situation. I have a table with UUID (unique), email (repeated), timestamp (unique) and has_sales (can be 1 if yes and 0 if no)
Sample data
uuid email timestamp has_sales
1 [email protected] 2016-10-02 10:28:23 0
2 [email protected] 2017-10-03 10:28:23 0
3 [email protected] 2017-10-06 17:08:15 1
4 [email protected] 2017-12-04 20:47:17 0
5 [email protected] 2018-05-21 15:27:04 0
6 [email protected] 2016-10-02 10:28:23 1
7 [email protected] 2017-10-03 10:28:23 0
I would like to choose the oldest timestamp, except when there is a sale on a newer one (it is rare, but it may occur). So, the expected result would be
uuid email timestamp has_sales
3 [email protected] 2017-10-06 17:08:15 1
6 [email protected] 2016-10-02 10:28:23 1
Currently, I am just using the first condition (oldest timestamp), as follows:
SELECT
dm1.uuid,
dm1.email,
dm1.timestamp,
dm1.has_sales
FROM dup_mail dm1
where
time_stamp = (select min(time_stamp)
from dup_mail dm2
where dm1.email = dm2.email
)
order by 2
How, upgrading this code, could I add the condition of if there is sale to a newer user and no sale to the older one, I would choose the newer one? Each email is related to either no sales (0 in all duplicate accounts) or yes sale (1 in one of the duplicate accounts and 0 in the other ones). Even if there is more than one duplicate account with sales, I just want to know if there was a sale or not