Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Help with simple sql statement usiing Rank function..

$
0
0

Hello I have very important question..this is how i have a dataset...

This is the Log Table 

And the actual Table holds the current value.   12345

Current Table 

12345   575 232 4567

Log Table Values

Objectid InvalidatedTimePhoneAreaPhoneno Rank

12345  2014-04-23570         822-4438     0  3
12345  2014-04-28NULL658-7789      0 2
12345  2014-05-09 578       658-8521     0 1

As you can see on 23 they invlalidated the areacode and phone no and changed it to .. 578 658-8521 but on 04-28 they just changed the phone no and not the area code.. 

I want the result set to be like this

DateFromvalue   ToValue  

2014-04-23 570-822-4438        578 658-7789

2014-04-28 578 658-7789 578 658-8521

2014-05-09 578 658-8521575 232 4567

 I have tried this code.. 

select 
--'Home Phone',
--tt.invalidationtime ,
--Case when 
--tt.*,tt_prev.* --,
'Home Phone' as PropertyName,
tt.invalidationtime,
case when tt.rank1 = 1 then tt.phoneareacode+'-'+tt.phoneno
when tt.rank1 <> 1 then Isnull(tt.phoneareacode,tt_prev.phoneareacode)+'-'+ isnull(tt.phoneno,tt_prev.phoneno)
end as FromAddress,

case when tt.rank1 = 1 then Haddress.phoneareacode+'-'+HAddress.phoneno
when tt.rank1 <> 1 then Isnull(tt_prev.phoneareacode,tt.phoneareacode)+'-'+isnull(tt_prev.phoneno,tt.phoneno)
end as ToPhone
from
 #Phone_Final  tt
left join  #Phone_Final tt_prev on tt_prev.objectid = tt.objectid and tt.rank1-11 = tt_prev.rank1
inner join (select * from Soarian_Clin_Prd_1.dbo.Haddress   where PP_id = 2346   and Addresstype = 0) Haddress

Can you please tell me what is wrong here.. 



Viewing all articles
Browse latest Browse all 23857

Trending Articles