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] "mak" <mak@discussions.microsoft.com> wrote in message
news:BE61D835-5E0B-4496-9761-33F8E6927371@microsoft.com...
> 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.
>
>
> "Ashish Nanda" wrote:
>
> > 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
> >
> >
> >