使用Postgis函数编写SQL查询时输出错误

时间:2014-11-11 19:05:15

标签: php postgresql subquery postgis psql

我正在完成我的作业,而且我已经完成了它。问题是我说的最后一个查询只能返回10行,但我现在所拥有的返回16行,我无法弄清楚我做错了什么。查询的定义是

Find all urban areas that (1) have a combined land & water area of greater than 1500 square 
kilo- meters and (2) intersect multiple states. Your query should return the urban area name 
(name10) and a count of the number of states intersected. The results should be first ordered by 
the number of states intersected (in descending order) and secondarily by alphabetical order of 
the urban area names from (A to Z). (Note: Pay very close attention to the units used for the 
areas in this question.) (10 records) 

我写的返回16条记录的SQL查询是

SELECT area1.name10, COUNT(*) AS Count 
FROM tl_2010_us_uac10 AS area1, tl_2010_us_state10 AS area2 
WHERE ((area1.aland10 + area1.awater10)/1000) > 1500 
AND ST_Intersects(area1.coords, area2.coords) 
GROUP BY area1.name10 HAVING COUNT(*) > 2 
ORDER BY Count DESC, area1.name10 ASC;

我正在使用的两个表是

Table "public.tl_2010_us_state10"
    Column   |            Type             |                            Modifiers                             
 ------------+-----------------------------+-------------------------------------
  gid        | integer                     | not null default 
  region10   | character varying(2)        | 
  division10 | character varying(2)        | 
  statefp10  | character varying(2)        | 
  statens10  | character varying(8)        | 
  geoid10    | character varying(2)        | 
  stusps10   | character varying(2)        | 
  name10     | character varying(100)      |
  lsad10     | character varying(2)        | 
  mtfcc10    | character varying(5)        | 
  funcstat10 | character varying(1)        | 
  aland10    | double precision            | 
  awater10   | double precision            | 
  intptlat10 | character varying(11)       | 
  intptlon10 | character varying(12)       | 
  coords     | geometry(MultiPolygon,4326) | 

 Indexes:
 "tl_2010_us_state10_pkey" PRIMARY KEY, btree (gid)
 "tl_2010_us_state10_coords_gist" gist (coords)

然后这是城市信息表。

                                      Table "public.tl_2010_us_uac10"
    Column   |            Type             |                           Modifiers                         
 ------------+-----------------------------+-------------------------------------
 gid        | integer                     | not null default 
 uace10     | character varying(5)        | 
 geoid10    | character varying(5)        | 
 name10     | character varying(100)      |
 namelsad10 | character varying(100)      | 
 lsad10     | character varying(2)        | 
 mtfcc10    | character varying(5)        | 
 uatyp10    | character varying(1)        | 
 funcstat10 | character varying(1)        | 
 aland10    | double precision            | 
 awater10   | double precision            | 
 intptlat10 | character varying(11)       | 
 intptlon10 | character varying(12)       | 
 coords     | geometry(MultiPolygon,4326) | 

 Indexes:
"tl_2010_us_uac10_pkey" PRIMARY KEY, btree (gid)
"tl_2010_us_uac10_coords_gist" gist (coords)

提前致谢

1 个答案:

答案 0 :(得分:1)

面积以平方米计。要将平方米转换为平方公里,除以1,000,000 - 而不是1,000。

1 km = 1,000m。 1km 2 =(1,000m)** 2 = 1,000,000(m 2)