sql server - SQL: Return multiple oldest records only -


i'm attempting return string using complicated bit of sql, cant return based on same phonenumber newest d.createdatetime, instance

source           calldata                       phonenum    fk_ref  d.createdatetime source   1 609 ^mr richard smith^01234567891^   01234567891 27657   16/06/2014 source2  1 609 ^mr richard smith^01234567891^   01234567891 27657   21/07/2014 source3  1 609 ^mr richard smith^01234567891^   01234567891 27657   03/10/2014 

expected result

    source           calldata                       phonenum    fk_ref  d.createdatetime     source3  1 609 ^mr richard smith^01234567891^   01234567891 27657   03/10/2014 

however can't seem find way effectively, attempts using

sql server: select rows max(date) , t-sql select rows oldest date , unique category

does not work, or rather can't find place insert said data query.

if can understand (somewhat complicated) sql query enough help, beyond amazing.

thank you

code below

--create table #tmp2(fk_clientids varchar(50)) --create table #tmp (phonenums varchar(50)) delete #tmp2 delete #tmp use davikertwf_mta insert #tmp2 select fk_clientid dm_clientapplicants fk_applicationid in (--fk_applicationid goes here)  use davikertwf_ots insert #tmp select phonenum2 dm_phonenumbers fk_applicationid in  (    select fk_clientids #tmp2 ) insert #tmp select phonenum1 dm_phonenumbers fk_applicationid in  (    select fk_clientids #tmp2 ) insert #tmp select phonenum2 dm_phonenumbers fk_applicationid in  (    select fk_clientids #tmp2 )  insert #tmp select phonenum3 dm_phonenumbers fk_applicationid in  (    select fk_clientids #tmp2 ) insert #tmp select partnerphonehome dm_phonenumbers fk_applicationid in  (    select fk_clientids #tmp2 ) insert #tmp select partnerphonemobile dm_phonenumbers fk_applicationid in  (    select fk_clientids #tmp2 ) insert #tmp select partnerphonework dm_phonenumbers fk_applicationid in  (    select fk_clientids #tmp2 ) use touchstar select sourcetable,calldata,phonenum,dm_phonenumbers.fk_applicationid,d.createdatetime dial d join davikertwf_ots.dbo.dm_phonenumbers on phonenum= phonenum1 collate latin1_general_ci_as phonenum in ( select phonenums #tmp ) 

there few ways can this. way use row_number function order rows partitioned key interested in, , select top one. this:

select sourcetable,calldata,phonenum,fk_applicationid,createdatetime (     select sourcetable,calldata,phonenum,dm_phonenumbers.fk_applicationid,d.createdatetime, row_number() on (partition phonenum order d.createdatetime desc) rownum     dial d     join davikertwf_ots.dbo.dm_phonenumbers      on phonenum= phonenum1 collate latin1_general_ci_as     phonenum in         (         select phonenums #tmp         ) ) allrows rownum = 1 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -