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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -