У моего клиента есть регионы продаж, где каждый регион продаж состоит из списка почтовых индексов.Регионы довольно большие и их легче хранить в формате, подобном следующему:
Область состоит из диапазона почтового индекса от 00602 до 10012 и от 20020 до 30020.
Как получить список из почтового индексакоды к списку таких диапазонов почтовых индексов?
Рассмотрим следующие данные
--This would be my list of all available zip codes in us:
CREATE TABLE [Zip](
[Zip] [nvarchar](20) ,
[State] [nvarchar](50) ,
)
--This would be the Sales Region List
CREATE TABLE [dbo].[SalesRegion](
[AreaCode] [nvarchar](50)
)
--This would be the original large list Zip Codes for the SalesRegions
CREATE TABLE [dbo].[EnteredZip](
[Zip] [nvarchar](20) ,
[AreaCode] [nvarchar](50)
)
--This is where I would like to store the Zip Code Ranges
CREATE TABLE [dbo].[SearchableZip](
[StartZip] [nvarchar](20) ,
[EndZip] [nvarchar](20) ,
[AreaCode] [nvarchar](50)
)
--Here is my sample Data:
--Some Zip Codes in US
insert into dbo.Zip (Zip,[State]) values ('00501' ,'PR')
insert into dbo.Zip (Zip,[State]) values ('00544' ,'PR')
insert into dbo.Zip (Zip,[State]) values ('00601' ,'PR')
insert into dbo.Zip (Zip,[State]) values ('00602' ,'PR')
insert into dbo.Zip (Zip,[State]) values ('00603' ,'PR')
insert into dbo.Zip (Zip,[State]) values ('00604' ,'PR')
insert into dbo.Zip (Zip,[State]) values ('00605' ,'PR')
insert into dbo.Zip (Zip,[State]) values ('00606' ,'PR')
insert into dbo.Zip (Zip,[State]) values ('00610' ,'PR')
insert into dbo.Zip (Zip,[State]) values ('00611' ,'PR')
insert into dbo.Zip (Zip,[State]) values ('00612' ,'PR')
--Some Sales Regions
Insert Into dbo.SalesRegion ( AreaCode ) values('Area1')
Insert Into dbo.SalesRegion ( AreaCode ) values('Area2')
Insert Into dbo.SalesRegion ( AreaCode ) values('Area3')
--The zip codes of the Sales Regions
insert Into EnteredZip (Zip,AreaCode) values ('00544' , 'Area1')
insert Into EnteredZip (Zip,AreaCode) values ('00601' , 'Area1')
insert Into EnteredZip (Zip,AreaCode) values ('00602' , 'Area1')
insert Into EnteredZip (Zip,AreaCode) values ('00604' , 'Area2')
insert Into EnteredZip (Zip,AreaCode) values ('00606' , 'Area2')
insert Into EnteredZip (Zip,AreaCode) values ('00501' , 'Area3')
insert Into EnteredZip (Zip,AreaCode) values ('00544' , 'Area3')
insert Into EnteredZip (Zip,AreaCode) values ('00601' , 'Area3')
insert Into EnteredZip (Zip,AreaCode) values ('00602' , 'Area3')
insert Into EnteredZip (Zip,AreaCode) values ('00603' , 'Area3')
insert Into EnteredZip (Zip,AreaCode) values ('00604' , 'Area3')
insert Into EnteredZip (Zip,AreaCode) values ('00610' , 'Area3')
insert Into EnteredZip (Zip,AreaCode) values ('00611' , 'Area3')
insert Into EnteredZip (Zip,AreaCode) values ('00612' , 'Area3')
Приведем к этой записи в таблице SearchableZip
AreaCode StartZip EndZip
-------------------- -------------------- -------------------------
Area1 00544 00602
Area2 00604 00604
Area2 00606 00606
Area3 00501 00604
Area3 00610 00612
Можно ли создать SearchableZip с помощью сценария sql?
РЕДАКТИРОВАТЬ
Я исправил объявление таблицы и выходные данные