if OBJECT_ID('tempdb..#ctryarea') is not null begin drop table tempdb..#ctryarea end select c.CountryId , c.BUnitId , c.CountryCode , coalesce(a.AreaName, N'N/A') as AreaName into #ctryarea from Geography.Country as c left join Geography.AreaCountry as ac on c.CountryId = ac.CountryId left join Geography.Area as a on ac.AreaId = a.AreaId if OBJECT_ID('tempdb..#ctrywarea') is not null begin drop table #ctrywarea end select bu.BUnitId , c.CountryId , c.CountryCode , ci.CityName , wazr.StartZipCode , wazr.EndZipCode into #ctrywarea from Common.BUnit as bu inner join Geography.Country as c on bu.CountryCode = c.CountryCode and bu.BUnitId = c.BUnitId left join Geography.WorkArea as wa on c.CountryId = wa.CountryId left join Geography.WorkAreaZipcodeRange as wazr on wa.WorkAreaId = wazr.WorkAreaId left join Geography.City as ci on wa.CityId = ci.CityId create index ctraywarea1 on #ctrywarea (BUnitId, CountryCode, StartZipCode) include (EndZipCode, CityName) if OBJECT_ID('tempdb..#ctryzone') is not null begin drop table tempdb..#ctryzone end select bu.BUnitId , c.CountryId , c.CountryCode , cz.ZoneName , czr.StartZipCode , czr.EndZipCode into #ctryzone from Common.BUnit as bu inner join Geography.Country as c on bu.CountryCode = c.CountryCode and bu.BUnitId = c.BUnitId left join Geography.CountryZone as cz on c.CountryId = cz.CountryId left join Geography.CountryZoneRange as czr on cz.CountryZoneId = czr.ZoneId create index ctryzone1 on #ctryzone (BUnitId, CountryCode, StartZipCode) include (EndZipCode, ZoneName) if OBJECT_ID('tempdb..#allcodes') is not null begin drop table tempdb..#allcodes end ;with num as ( select ROW_NUMBER() over(order by (select null)) as n from sys.all_columns as ac1, sys.all_columns as ac2 ), allcodes as ( select bu.BUnitId, bu.CountryCode , n-1 as ZipCode from Common.BUnit as bu, num where bu.CountryCode = N'LV' and n <= 10000 union all select bu.BUnitId, bu.CountryCode , n-1 as ZipCode from Common.BUnit as bu, num where bu.CountryCode = N'LT' and n <= 100000 union all select bu.BUnitId, bu.CountryCode , n-1 as ZipCode from Common.BUnit as bu, num where bu.CountryCode = N'EE' and n <= 100000 ) select * into #allcodes from allcodes if OBJECT_ID('tempdb..#result') is not null begin drop table tempdb..#result end ;with res as ( select N'DOM_' + cast(allcodes.ZipCode as nvarchar(10)) as GeographyKey , allcodes.BUnitId , allcodes.CountryCode , cast(allcodes.ZipCode as int) as ZipCode , (select top 1 wa.CityName from #ctrywarea as wa with(index (ctraywarea1)) where allcodes.BUnitId = wa.BUnitId and allcodes.ZipCode >= wa.StartZipCode and wa.EndZipCode >= allcodes.ZipCode and wa.CountryCode = allcodes.CountryCode) as CityName , other.ZoneName , other.StartZipCode , other.EndZipCode , (select top 1 ac.AreaName from #ctryarea as ac where ac.BUnitId = allcodes.BUnitId and ac.CountryCode = allcodes.CountryCode) as AreaCountry from #allcodes as allcodes outer apply ( select top 1 cz.ZoneName, cz.StartZipCode, cz.EndZipCode from #ctryzone as cz with (index (ctryzone1)) where allcodes.BUnitId = cz.BUnitId and allcodes.ZipCode >= cz.StartZipCode and cz.EndZipCode >= allcodes.ZipCode and cz.CountryCode = allcodes.CountryCode and cz.ZoneName not like '%DPD EXP ZONE%' ) as other union all select cast(czr.ZoneRangeKey as nvarchar(20)) as GeographyKey , cz.BUnitId , cz.CountryCode , -1 as ZipCode , N'N/A' as CityName , cz.ZoneName , czr.StartZipCode , czr.EndZipCode --, coalesce(a.AreaName, N'N/A') as AreaCountry , coalesce((select top 1 AreaName from Geography.AreaCountry as ac left join Geography.Area as a on ac.AreaId = a.AreaId where cz.CountryId = ac.CountryId order by a.areakey), N'N/A') as AreaCountry from Geography.CountryZoneRange as czr inner join Geography.CountryZone as cz on czr.ZoneId = cz.CountryZoneId inner join Common.BUnit as bu on cz.BUnitId = bu.BUnitId --left join Geography.AreaCountry as ac on cz.CountryId = ac.CountryId --left join Geography.Area as a on ac.AreaId = a.AreaId where cz.CountryCode <> bu.CountryCode union all select cast(N'CTRY_' + cast(c.CountryId as nvarchar(10)) as nvarchar(20)) as GeographyKey , c.BUnitId , CountryCode , -1 as ZipCode , N'N/A' as CityName , N'N/A' as ZoneName , -1 as StartZipCode , -1 as EndZipCode --, coalesce(a.AreaName, N'N/A') as AreaCountry , coalesce((select top 1 AreaName from Geography.AreaCountry as ac left join Geography.Area as a on ac.AreaId = a.AreaId where c.CountryId = ac.CountryId order by a.areakey), N'N/A') as AreaCountry from Geography.Country as c --left join Geography.AreaCountry as ac on c.CountryId = ac.CountryId --left join Geography.Area as a on ac.AreaId = a.AreaId ) insert into Stage.Geography_Geography (GeographyKey, BUnitId, CountryCode, ZipCode, StartZipCode, EndZipCode, CityName, ZoneName, AreaCountry) select GeographyKey , BUnitId , CountryCode , coalesce(ZipCode, -1) as ZipCode , coalesce(StartZipCode, -1) as StartZipCode , coalesce(EndZipCode, -1) as EndZipCode , coalesce(CityName, N'N/A') as CityName , coalesce(ZoneName, N'N/A') as ZoneName , coalesce(AreaCountry, N'N/A') as AreaCountry from res