Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 QUERY HELP

Author  Topic 

nhess80
Yak Posting Veteran

83 Posts

Posted - 2010-03-22 : 17:44:53
So the query I have below is almost complete. I just need to figure out one more part I am having trouble with and I’m not really sure how to go about doing it or how difficult it will be.


Declare @LergData table(lata varchar(4),ocn varchar(4),npa varchar(3),nxx varchar(3),y varchar(2),TBPI varchar(1))
insert into @LergData select '224','4804',201,204,0,'Y'
insert into @LergData select '224','8700',201,204,1,'Y'
insert into @LergData select '224','6664',201,204,2,'Y'
insert into @LergData select '224','325E',201,204,3,'Y'
insert into @LergData select '224','6664',201,204,6,'Y'
insert into @LergData select '224','6664',201,204,7,'Y'
insert into @LergData select '224','6623',201,204,8,'Y'
insert into @LergData select '224','4804',201,204,9,'Y'
insert into @LergData select '224','389C',201,204,'A','Y'
insert into @LergData select '224','6391',201,207,'A','N'


Declare @OCN Table (lata varchar(4),ocn varchar(4),inter decimal(18, 7),intra decimal(18, 7))
insert into @OCN select '224','325E',0.0167,0.0167
insert into @OCN select '224','389C',0.0165,0.0165
insert into @OCN select '224','4804',0.0155,0.0155
insert into @OCN select '224','6623',0.0145,0.0145
insert into @OCN select '224','6664',0.0167,0.0167
insert into @OCN select '224','8700',0.0165,0.0165
insert into @OCN select '224','6391',0.0135,0.0135



SELECT c.NPANXX,o.inter,o.intra

FROM @OCN AS o
INNER JOIN

(SELECT DISTINCT b.NPANXX,b.lata,b.ocn

FROM @LergData AS a
INNER JOIN

(SELECT CASE
WHEN TBPI = 'Y' AND y = 'A' THEN NULL
WHEN TBPI = 'Y' THEN npa+''+nxx+''+y
ELSE npa+''+nxx
END AS NPANXX,lata,ocn
FROM @LergData b
Group by npa+''+nxx,npa+''+nxx+''+y,y,TBPI,lata,ocn) b

ON a.npa+''+a.nxx = LEFT(b.NPANXX,6)) c
ON c.lata = o.lata AND c.ocn = o.ocn

ORDER BY NPANXX




RESULTS
-------------------------------------

NPANXX inter intra
2012040 0.0155000 0.0155000
2012041 0.0165000 0.0165000
2012042 0.0167000 0.0167000
2012043 0.0167000 0.0167000
2012046 0.0167000 0.0167000
2012047 0.0167000 0.0167000
2012048 0.0145000 0.0145000
2012049 0.0155000 0.0155000
201207 0.0135000 0.0135000


I need to figure out how to fill in the holes. As you can see from the results I have 2012040,2012041,2012042 and so on. But I am missing 2012044 and 2012045. I need to figure out how to fill those in and assign the default rate which in this case would be 0.0165 because of this

insert into @LergData select '224','389C',201,204,'A','Y'

insert into @OCN select '224','389C',0.0165,0.0165

The number 201207 will not need this done since there are no extra numbers out to the 7th digit. It will be left alone

DESIRED RESULTS
------------------------------------------------
NPANXX inter intra
2012040 0.0155000 0.0155000
2012041 0.0165000 0.0165000
2012042 0.0167000 0.0167000
2012043 0.0167000 0.0167000
2012044 0.0165000 0.0165000
2012045 0.0165000 0.0165000
2012046 0.0167000 0.0167000
2012047 0.0167000 0.0167000
2012048 0.0145000 0.0145000
2012049 0.0155000 0.0155000
201207 0.0135000 0.0135000


If anyone has any ideas please let me know and thanks for your help

-Nick

nhess80
Yak Posting Veteran

83 Posts

Posted - 2010-03-23 : 17:05:16
Figured out a solution
Go to Top of Page
   

- Advertisement -