Groups | Blog | Home
all groups > sql server programming > july 2004 >

sql server programming : Querying a database


Ashish Nanda
7/23/2004 7:15:17 PM
I have my table like this. The zip code column has zip
code in 9 digits.

Zipcode Code
056790000 A101
056800000 A101
056810000 A101
056820000 A101
056890000 A101
056900000 A102
056901111 A102
056901168 A103
056910000 A102
056920000 A102
.....
....
I have data like 100000 records in this view/table. I
think the data format in the table is clear, if not i can
provide more info


This is how i want the output to be.... I can have two
columns to so i can have it like

ZipStart ZipEnd Code
056790000 056890000 A101
056900000 056901111 A102
056901168 056901168 A103
056910000 056920000 A102

Can someone suggest a query or a procedure ?The code is
going to run on a table with more then 80000 records.May
be a set based solution or something this is urgent...
thanks in advance

Steve Kass
7/24/2004 12:37:46 AM
Ashish,

See if something like this is efficient enough (and do some careful
testing - I didn't):

create table Zips (
zipcode char(9) primary key,
Code char(4)
)
insert into Zips values ('056790000','A101')
insert into Zips values ('056800000','A101')
insert into Zips values ('056810000','A101')
insert into Zips values ('056820000','A101')
insert into Zips values ('056890000','A101')
insert into Zips values ('056900000','A102')
insert into Zips values ('056901111','A102')
insert into Zips values ('056901168','A103')
insert into Zips values ('056910000','A102')
insert into Zips values ('056920000','A102')
go


select
min(zipcode) as ZipStart,
ZipEnd
from (
select
Z1.zipcode,
max(Z2.zipcode) as ZipEnd
from Zips Z1 left outer join Zips Z2
on Z1.zipcode <= Z2.zipcode
and Z2.zipcode < (
select top 1 Z3.zipcode
from Zips Z3
where Z3.zipcode > Z1.zipcode
and Z3.Code <> Z1.Code
order by Z3.zipcode
)
group by Z1.zipcode
) Z
group by ZipEnd
order by 1

drop table Zips


Steve Kass
Drew University

[quoted text, click to view]
mak
7/24/2004 3:23:02 AM
hi
UDF is the best solution for this.
Query ..i think u ll face performance issue,if the data is huge.
so go for UDF.


[quoted text, click to view]
Mischa Sandberg
7/24/2004 10:25:00 PM
Ummm, there's a method that works on 10M+ row tables, which UDF's don't
really.
This method may be overkill, but the big joins are strictly
MERGE JOINS, which have very predictable performance.

For production batches, I always include audit queries to assure
everyone that IDENTITY really does produce contiguous integers
in exactly the right sequence. Haven't been disappointed yet ...
----------------------------------------------------------------------------
---------------------
-- Assume TABLE ZipMap(zipcode varchar(9), code varchar(9));
-- doesn't really matter what their types are

-- Number the rows sequentially, by ascending zip code.
CREATE UNIQUE CLUSTERED INDEX uc_zipcode ON ZipMap(zipcode)
SELECT IDENTITY(INT,1,1) AS seq, * INTO ZipMap2 FROM ZipMap

-- Step 1: find the boundaries between runs
-- (azip...) is the end of a run, (bzip...) is the start of the next run

CREATE UNIQUE CLUSTERED INDEX uc_seq ON ZipMap2(seq)
SELECT A.zipcode AS azip, A.code AS acode,
B.zipcode AS bzip, B.code AS bcode
INTO ZipMap3
FROM ZipMap2 A
JOIN ZipMap2 B ON B.seq = A.seq+1 AND B.code <> A.code

-- Don't forget the start of the very first run, and the end of the last
run:
INSERT ZipMap3
SELECT TOP 1 '', '', zipcode, code
FROM ZipMap2 ORDER BY seq

INSERT ZipMap3
SELECT TOP 1 zipcode, code, 'zzz', ''
FROM ZipMap2 ORDER BY seq DESC

-- Step 2: sequentially number each (end,begin) range,
-- so you can match it up with following row:
CREATE UNIQUE CLUSTERED INDEX uc_azip ON ZipMap3(azip)
SELECT IDENTITY(INT,1,1) AS seq, * INTO ZipMap4 FROM ZipMap3

CREATE UNIQUE CLUSTERED INDEX uc_seq ON ZipMap4(seq)

SELECT A.bzip AS ZipStart, B.azip AS ZipEnd, A.bcode AS Code
FROM ZipMap4 A JOIN ZipMap4 B ON B.seq = A.seq+1

[quoted text, click to view]

AddThis Social Bookmark Button