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.0167insert into @OCN select '224','389C',0.0165,0.0165insert into @OCN select '224','4804',0.0155,0.0155insert into @OCN select '224','6623',0.0145,0.0145insert into @OCN select '224','6664',0.0167,0.0167insert into @OCN select '224','8700',0.0165,0.0165insert into @OCN select '224','6391',0.0135,0.0135SELECT c.NPANXX,o.inter,o.intraFROM @OCN AS oINNER JOIN(SELECT DISTINCT b.NPANXX,b.lata,b.ocnFROM @LergData AS aINNER 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,ocnFROM @LergData bGroup by npa+''+nxx,npa+''+nxx+''+y,y,TBPI,lata,ocn) b ON a.npa+''+a.nxx = LEFT(b.NPANXX,6)) cON c.lata = o.lata AND c.ocn = o.ocnORDER BY NPANXX
RESULTS-------------------------------------NPANXX inter intra2012040 0.0155000 0.01550002012041 0.0165000 0.01650002012042 0.0167000 0.01670002012043 0.0167000 0.01670002012046 0.0167000 0.01670002012047 0.0167000 0.01670002012048 0.0145000 0.01450002012049 0.0155000 0.0155000201207 0.0135000 0.0135000I 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 thisinsert into @LergData select '224','389C',201,204,'A','Y'insert into @OCN select '224','389C',0.0165,0.0165The number 201207 will not need this done since there are no extra numbers out to the 7th digit. It will be left aloneDESIRED RESULTS------------------------------------------------NPANXX inter intra2012040 0.0155000 0.01550002012041 0.0165000 0.01650002012042 0.0167000 0.01670002012043 0.0167000 0.01670002012044 0.0165000 0.01650002012045 0.0165000 0.01650002012046 0.0167000 0.01670002012047 0.0167000 0.01670002012048 0.0145000 0.01450002012049 0.0155000 0.0155000201207 0.0135000 0.0135000If anyone has any ideas please let me know and thanks for your help-Nick