Postgresql 9.x上的邮政编码之间的Postgis ST_Distance

时间:2014-03-09 16:29:54

标签: sql postgresql postgis

这更像是一个SQL问题,而不是PostGIS问题,但我再次陷入困境:(

我在“from”和“to”列中有一个名为id的表。 我想根据邮政编码计算所有这些身份证号码之间的距离。

有一个名为doc的单独参考表,其中包含“NPI”列中的ID号和“提供商业务邮件地址邮政编码”列中的邮政编码和一个名为zctas的单独地理表,其中邮政编码列为zcta和geom列。

例如,此查询可以正常工作:

SELECT z.zcta As zip1, 
       z2.zcta As zip2, 
       ST_Distance(z.geom,z2.geom) As thedistance 
  FROM zctas z, 
       zctas z2 
 WHERE z2.zcta = '60611' 
       AND z.zcta='19611';

一个问题是“提供商商业邮寄地址邮政编码”应该=左(“提供商商业邮寄地址邮政编码”,5)。

在这一个查询中,我一直停留在参考表中加入2个邮政编码。

样本表:

参考表:

    from    |     to     | count 
------------+------------+-------
 1174589766 | 1538109665 |   108
 1285653204 | 1982604013 |    31

期望的输出:

    from    |     to     | count | distance
------------+------------+----------------
 1174589766 | 1538109665 |   108 | 53434
 1285653204 | 1982604013 |    31 | 34234

\ d +

Table "public.zctas"
          Column      |          Type          | Modifiers | Storage  | Stats target | Description 
    ------------------+------------------------+-----------+----------+--------------+-------------
     state            | character(2)           |           | extended |              | 
     zcta             | character(5)           |           | extended |              | 
     junk             | character varying(100) |           | extended |              | 
     population_tot   | bigint                 |           | plain    |              | 
     housing_tot      | bigint                 |           | plain    |              | 
     water_area_meter | double precision       |           | plain    |              | 
     land_area_meter  | double precision       |           | plain    |              | 
     water_area_mile  | double precision       |           | plain    |              | 
     land_area_mile   | double precision       |           | plain    |              | 
     latitude         | double precision       |           | plain    |              | 
     longitude        | double precision       |           | plain    |              | 
     thepoint_lonlat  | geometry(Point,4269)   |           | main     |              | 
     thepoint_meter   | geometry(Point,32661)  | not null  | main     |              | 
     geom             | geometry(Point,32661)  |           | main     |              | 
    Indexes:
        "idx_zctas_thepoint_lonlat" gist (thepoint_lonlat)
        "idx_zctas_thepoint_meter" gist (thepoint_meter) CLUSTER

                              Table "public.referred"
 Column |         Type          | Modifiers | Storage  | Stats target | Description 
--------+-----------------------+-----------+----------+--------------+-------------
 from   | character varying(25) |           | extended |              | 
 to     | character varying(25) |           | extended |              | 
 count  | integer               |           | plain    |              | 
Has OIDs: no

        Table "public.doc"
                            Column                            |          Type          | Modifiers | Storage  | Stats target | Description 
--------------------------------------------------------------+------------------------+-----------+----------+--------------+-------------
 NPI                                                          | character varying(255) |           | extended |              | 
 Entity Type Code                                             | character varying(255) |           | extended |              | 
 Replacement NPI                                              | character varying(255) |           | extended |              | 
 Employer Identification Number (EIN)                         | character varying(255) |           | extended |              | 
 Provider Organization Name (Legal Business Name)             | character varying(255) |           | extended |              | 
 Provider Last Name (Legal Name)                              | character varying(255) |           | extended |              | 
 Provider First Name                                          | character varying(255) |           | extended |              | 
 Provider Middle Name                                         | character varying(255) |           | extended |              | 
 Provider Name Prefix Text                                    | character varying(255) |           | extended |              | 
 Provider Name Suffix Text                                    | character varying(255) |           | extended |              | 
 Provider Credential Text                                     | character varying(255) |           | extended |              | 
 Provider Other Organization Name                             | character varying(255) |           | extended |              | 
 Provider Other Organization Name Type Code                   | character varying(255) |           | extended |              | 
 Provider Other Last Name                                     | character varying(255) |           | extended |              | 
 Provider Other First Name                                    | character varying(255) |           | extended |              | 
 Provider Other Middle Name                                   | character varying(255) |           | extended |              | 
 Provider Other Name Prefix Text                              | character varying(255) |           | extended |              | 
 Provider Other Name Suffix Text                              | character varying(255) |           | extended |              | 
 Provider Other Credential Text                               | character varying(255) |           | extended |              | 
 Provider Other Last Name Type Code                           | character varying(255) |           | extended |              | 
g(255) |           | extended |              | 
 Provider Second Line Business Mailing Address                | character varying(255) |           | extended |              | 
 Provider Business Mailing Address City Name                  | character varying(255) |           | extended |              | 
 Provider Business Mailing Address State Name                 | character varying(255) |           | extended |              | 
 Provider Business Mailing Address Postal Code                | character varying(255) |           | extended |         . . . . other columns not really needed.

感谢!!!!

1 个答案:

答案 0 :(得分:1)

这应该相对简单。

假设NPI在docreferred中实际上具有相同的长度,您可以非常轻松地加入这些表:

SELECT ad."Provider Business Mailing Address Postal Code" as a_zip,
       bd."Provider Business Mailing Address Postal Code" as b_zip,
       r."count"
  FROM referred r
           LEFT JOIN doc ad ON r."from" = ad."NPI"
           LEFT JOIN doc bd ON r."from" = bd."NPI";

显然,请根据对数据中NPIfrom / to字段的仔细分析来调整此联接。如有必要,在联接中添加trimleft方法调用 - 最重要的是JOIN条件是可比数据。

现在,从这个到原始查询找到距离是微不足道的:

SELECT ad."Provider Business Mailing Address Postal Code" as a_zip,
       bd."Provider Business Mailing Address Postal Code" as b_zip,
       r."count",
       ST_Distance(az.geom,bz.geom) As thedistance
  FROM referred r
           LEFT JOIN doc ad ON r."from" = ad."NPI"
           LEFT JOIN doc bd ON r."from" = bd."NPI"
           LEFT JOIN zctas az 
               ON az.zcta = left(ad."Provider Business Mailing Address Postal Code",5)
           LEFT JOIN zctas bz
               ON bz.zcta = left(bd."Provider Business Mailing Address Postal Code",5)

这只是一种应该有效的结构,其他许多都是可行的。这种特殊结构将确保表示referred中的每个条目,即使NPIdoc表中的条目不匹配,或者邮政编码无法匹配zctas表。

另一方面,如果NPI表中doc存在多个条目,那么提及此重复referred的任何NPI条目也将是复制。

同样,如果特定邮政编码(zctas)的zcta条目中有多个条目,您会看到重复的referred行。

这就是LEFT JOIN的工作方式,但我认为值得加入警告,因为提供商数据通常与NPI完全重复,并且邮政编码列表中的邮政编码通常是重复的邮政编码跨国界线。