我的客戶有銷售地區,其中每個銷售地區由郵政編碼列表組成。 區域是相當大的,並且可以更容易存儲格式,如:確定是否有幾個郵政編碼是連續的
地區包括郵政編碼範圍爲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
是否有可能與SQL腳本創建SearchableZip?
編輯
我固定的表聲明和輸出數據
的'插入EnteredZip ...'語句不一致。它們不匹配表格的聲明,並且列值的順序錯誤。 –
如果您遇到性能問題,這不是解決問題的方法。編輯您的問題,並粘貼查詢執行計劃,以查找效果不佳的查詢。就目前而言,它看起來像是在猜測解決方案。猜測不好。 –
你是對的,但我必須嘗試一下。即使它是一種糟糕的方式(它真的可能是) –