I have a scenario where i need to pick the latest Id of an employee . The id can be a varchar or int
id mail
-------------
1234 xyz@abc.com
5678 xyz@abc.com
C123 xyz@abc.com
C234 def@abc.com
Now, my query result just has to be
id mail
-------------
5678 xyz@abc.com
C234 def@abc.com
The logic is, If there is an integer against the mail id, the highest integer has to be picked. If there is no integer, the varchar value can be picked.
Tried
select max(id), mail from worker group by mail
But it picks the varchar value when there is a duplicate mail id.
I would use the isnumeric value and ROW_NUMBER to select the right value.
DECLARE @T TABLE (id VARCHAR(10), mail VARCHAR(100))
INSERT @T VALUES
('1234', 'xyz@abc.com'),
('5678', 'xyz@abc.com'),
('C123', 'xyz@abc.com'),
('C234', 'def@abc.com')
SELECT id, mail
FROM (
SELECT
id,
mail,
ROW_NUMBER() OVER (
PARTITION BY
mail
ORDER BY
CASE WHEN ISNUMERIC(id) = 1 THEN CONVERT(INT, id) ELSE 0 END DESC,
id) AS RowNum
FROM @T
) T
WHERE RowNum = 1
The output yields
id mail
---------- -------------
C234 def@abc.com
5678 xyz@abc.com
One solution I can think of is:
select MAX(t.id), t.email
from (
select * from Emails where ISNUMERIC(id) = 1
union
select * from Emails where ISNUMERIC(id) <> 1 and email not in
(select email from Emails where ISNUMERIC(id) = 1)
) as t
group by t.email
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.