THis is what i have - a query that retrieves all the Persons that have a duplicated email address believe rank over partition by should solve my problem (The filter is the Email Address)
SELECT a.Id, a.EmailAddress,a.UntilDate,a.CreatedOn,a.UserId
INTO #GetEmployeesWithDuplicateEmails
FROM Employee a
INNER JOIN (SELECT
Employee.EmailAddress as EmailAddress
FROM Employee
GROUP BY Employee.EmailAddress
HAVING count(Employee.EmailAddress) > 1
) b
ON a.EmailAddress = b.EmailAddress
ORDER BY a.Id
this is the output of the query Query Result
What i want - Query below retrieves the users that have duplicated EmailAddresses, i want to keep the most recent record by each email, if the email belongs to the same UserId of course, imagine that there are 5 duplicated emails, if i verify that those 5 duplicates belong to the same UserId i want to keep the newest record based on the CreatedOn field, the other 4 will be updated . I wanted to use rank over partition by, but you can advise me with a better scenario anyways here goes:
SELECT #GetEmployeesWithDuplicateEmails.*,
RANK() OVER (
PARTITION BY #GetEmployeesWithDuplicateEmails.CreatedOn
ORDER BY #GetEmployeesWithDuplicateEmails.CreatedOn DESC) createdon_rank
INTO #TableValuesToDelete
FROM #GetEmployeesWithDuplicateEmails
INNER JOIN
(
(SELECT #GetEmployeesWithDuplicateEmails.[EmailAddress]
FROM #GetEmployeesWithDuplicateEmails
GROUP BY #GetEmployeesWithDuplicateEmails.[EmailAddress])
) as temp2 ON #GetEmployeesWithDuplicateEmails.[EmailAddress]=temp2.[EmailAddress]
update
#TableValuesToUpdate
SET
#TableValuesToUpdate.EmployedUntilDate=getDate()
WHERE
created_rank > 1
i want to retain the most recent record by each email if the email belongs to the same UserId, Imagine that there are 5 duplicated emails, if i verify that those 5 duplicates belong to the same UserId i want to keep the newest record based on the CreatedOn field .
Update: Just Updated my Partion By query but it still can't rank the displayed values