I need to get all users from the database. When a user gets deleted, their column deletionDate
is populated with a date. When a deleted user is recreated, there is a new row created.
I need to retrieve all latest "versions" of each user, which means a row for every email. I wanted to get the one with deletionDate
at null if it exists else the one with the latest deletionDate
if no null row exists.
It's a mix between this Fetch the row which has the Max value for a column
And this : MySQL return max value or null if one column has no value
The pseudo table:
ID EMAIL DELETE_DATE
1 mail1 2016-09-08 09:56:21
2 mail1 2016-19-08 09:56:22
3 mail1 < SELECTED
4 mail2 2017-19-08 09:56:22
5 mail2 2018-19-08 09:56:22 < SELECTED
6 mail3 2018-15-08 09:56:22 < SELECTED
7 mail4 < SELECTED
SELECTED denotes the list of rows I would like to get in my query.
My DBMS is MySQL but if there is a way to do with JPA, it would be better and fit the criteria even better. Also I don't want to cherry-pick fields; I want the real rows.