sql server - Select old records in a table -
i want oldest row date each distinct number. created script problem keep on getting newest record.
select* [data].[dbo].[iapt] t1 [last contact date] in (select max([last contact date]) [data].[dbo].[iapt] t1.[number] = [data].[dbo].[iapt].[number] , [last contact date] not in (select max([last contact date]) [data].[dbo].[iapt] t1.[pseudo] = [data].[dbo].[iapt].[pseudo]))
the table:
pseudo number last contact date 0x1 18 17/06/2013 0x1 18 16/04/2013 0x2 19 25/04/2013 0x2 19 16/07/2013
desired result:
number last contact date 1 16/04/2013 2 25/04/2013
any appreciated. thank you
you should use min function instead of max function
select* [data].[dbo].[iapt] t1 [last contact date] in (select min([last contact date]) [data].[dbo].[iapt] t1.[number] = [data].[dbo].[iapt].[number] , [last contact date] not in (select min([last contact date]) [data].[dbo].[iapt] t1.[pseudo] = [data].[dbo].[iapt].[pseudo]))
Comments
Post a Comment