database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
ㆍPostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
PostgreSQL Q&A 3004 게시물 읽기
No. 3004
가장 근접한 ZIP code 찾기
작성자
정재익
작성일
2001-06-22 08:02
조회수
2,545

Submitted on: 05-06-2001

Description:

A way to locate nearest ZIP codes from a source ZIP code. [Phill Kenoyer]

Code:

 

Author: Phill Kenoyer

Date: Fri, 27 Apr 2001

License Type: Public Domain, Freeware

 

Requirment: To create a function in Postgres that will return nearest ZIP from a source ZIP.

 

ZIP code table:

 

CREATE TABLE "zips" (

"zip" varchar(10) NOT NULL,

"state" char(2) DEFAULT 'NA' NOT NULL,

"city" varchar(50) DEFAULT 'NA' NOT NULL,

"location" point DEFAULT '(0,0)' NOT NULL -- (longitude,latitude)

);

 

/* ZIP code data provided from: http://ftp.census.gov/geo/www/gazetteer/places.html */

 

Install the Earthdistance from the contrib section of Postgresql.

 

create function locate_providers (char(10),int2) returns text as '

declare

source_zip alias for $1;

offset alias for $2;

source_point point;

result record;

begin

-- Get the source point by zip

select into source_point location from zips where zip = source_zip;

 

if not found

then return ''Error: ZIP not found'';

end if;

 

-- Get the providers one at a time by offset

select into result

trim(p.name)::text as name, trim(p.address1)::text as address,

p.phone1::text as phone, trim(p.city)::text as city,

round(source_point <@> location)::text as distance

from zips z, providers p

where source_point <@> location < 50 and z.zip = p.zip

order by distance

limit 1, offset;

 

-- Return

if found

then return result.name || '', '' || result.address || '', '' ||

result.city || '', '' || result.phone || '', '' ||

result.distance || '' Miles'';

end if;

 

-- if nothing found

return ''Error: Provider Not Found'';

end;'

language 'plpgsql';

 

Now this works great for me and my project. My programs will have a loop to get the last 10 zips ie:

 

for ($x=0; $x<10; $x++)

{

$sql = "select locate_provider('95661',$x)";

$db->query($sql);

while ($db->fetch())

{

...

}

}

 

I hope this stuff helps!

[Top]
No.
제목
작성자
작성일
조회
3007Tuple is too big: size 8388, max size 8140 문제
답답한넘
2001-06-22
2326
3008┕>Re: Tuple is too big: size 8388, max size 8140 문제
정재익
2001-06-22 12:18:16
2701
3009 ┕>Re: Re: Tuple is too big: size 8388, max size 8140 문제
답답한넘
2001-06-22 13:27:57
2541
3010  ┕>Re: Re: Re: Tuple is too big: size 8388, max size 8140 문제
정재익
2001-06-22 14:31:29
2647
3029   ┕>Re: Re: Re: Re: Tuple is too big: size 8388, max size 8140 문제
답답한넘
2001-06-25 20:24:28
2565
3006PostgreSQL and iODBC
정재익
2001-06-22
2643
3005pgMail -- TCL-based scripts
정재익
2001-06-22
2229
3004가장 근접한 ZIP code 찾기
정재익
2001-06-22
2545
3003Number of affected rows
정재익
2001-06-22
1931
3002eval() for PostgreSQL
정재익
2001-06-22
1959
3001Retrive comma separated PK attributes
정재익
2001-06-22
1906
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.019초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다