Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id 36D7F9FB463 for ; Sun, 29 Oct 2006 13:14:50 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 05737-02 for ; Sun, 29 Oct 2006 17:14:44 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by postgresql.org (Postfix) with ESMTP id 5B3E89FB41A for ; Sun, 29 Oct 2006 13:11:01 -0400 (AST) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.8/8.13.8) with ESMTP id k9THAtgx024623 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sun, 29 Oct 2006 10:10:58 -0700 (MST) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.8/8.13.8) with ESMTP id k9THAtQJ071458; Sun, 29 Oct 2006 10:10:55 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.8/8.13.8/Submit) id k9THAtfe071457; Sun, 29 Oct 2006 10:10:55 -0700 (MST) (envelope-from mfuhr) Date: Sun, 29 Oct 2006 10:10:55 -0700 From: Michael Fuhr To: vamsee movva Cc: pgsql-sql@postgresql.org Subject: Re: not able to execute query on spatial database. Message-ID: <20061029171054.GA71279@winnie.fuhr.org> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=SPF_PASS, UNPARSEABLE_RELAY X-Spam-Level: X-Archive-Number: 200610/233 X-Sequence-Number: 26622 On Fri, Oct 27, 2006 at 09:34:21AM -0500, vamsee movva wrote: > I am working with spatial tables. i have two spatial data tables, Are you using PostGIS? If so then you might get more help on the postgis-users list. http://postgis.refractions.net/mailman/listinfo/postgis-users > one represents the whole state and another table represents the damaged > locations in the state. My aim is to find number of damaged locations in > every county or parish. > Here i am giving the query i used to do this, could you please tell me > whether i am doing right thing or not. > > select count(*) from damagedlocations l1,county l2 where (l2.the_geom and > l1.the_geom) and l2.parishid=particular_parishid; You might be looking for something like this; it should return all parish IDs that have damaged locations and the number of damaged locations in each parish: SELECT c.parishid, count(*) FROM county AS c JOIN damagedlocations AS d ON d.the_geom && c.the_geom AND distance(d.the_geom, c.the_geom) = 0 GROUP BY c.parishid; "d.the_geom && c.the_geom" restricts the result set based on bounding box overlaps; this expression can take advantage of indexes on the geometry columns so it's an efficient way to get a set of possible matches (put another way, an efficient way to eliminate impossible matches). "distance(d.the_geom, c.the_geom) = 0" does the more expensive work of finding certain matches. You could instead use "intersects(d.the_geom, c.the_geom)" but distance = 0 is often faster (if two geometries intersect then the distance between them is 0). -- Michael Fuhr