简体   繁体   English

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

[英]Postgis ST_Distance between zip codes on Postgress SQL 9.x

this is more of a SQL question than a PostGIS question, but I'm getting stuck again :( 这比PostGIS问题更像是一个SQL问题,但我再次陷入困境:(

I have a table called referred with id numbers in the "from" and "to" columns. 我在“从”和“到”列中有一个名为id的表。 I want to calculate the distance between ALL these id numbers based on their zip code. 我想根据它们的邮政编码计算所有这些ID号之间的距离。

There is a separate reference table called doc which contains the id number in column "NPI" and zip code in column "Provider Business Mailing Address Postal Code" and a separate geo table called zctas which has zip code column as zcta and geom column. 有一个名为doc的单独参考表,其中包含“ NPI”列中的ID号和“提供商业务邮寄地址邮政编码”列中的邮政编码,还有一个名为zctas的单独地理表,其邮政编码字段为zcta和geom列。

For example, this query works fine: 例如,此查询工作正常:

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';

One catch is that the "Provider Business Mailing Address Postal Code" should = left("Provider Business Mailing Address Postal Code", 5). 一个陷阱是“供应商业务邮寄地址邮政编码”应为left(“供应商业务邮寄地址邮政编码”,5)。

I'm getting stuck on JOIN-ing the 2 zip codes from the reference table in this one query. 在此查询中,我陷入了从引用表中加入2个邮政编码的困境。

Sample table: 样表:

referred table: 参照表:

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

desired output: 所需的输出:

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

\\d+ \\ 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.

Thanks!!!! 谢谢!!!!

This should be relatively straightforward. 这应该是相对简单的。

Assuming the NPIs are actually all the same length in doc and referred , you can join those tables quite easily: 假设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";

Obviously, adjust this join based on careful analysis of the NPI and from / to fields in your data. 显然,调整这个连接基础上,认真分析NPIfrom / to您的数据字段。 Add trim or left method calls within the join if necessary -- the most important thing is that the JOIN condition be on comparable data. 如有必要,在JOIN添加trimleft方法调用-最重要的是JOIN条件位于可比较的数据上。

Now, going from this to your original query to find a distance is trivial: 现在,从此查询到原始查询以查找距离很简单:

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)

This is just one construction that should work, many others are possible. 这只是一种应该起作用的构造,还有许多其他可能的构造。 This particular construction will ensure that every entry in referred is represented, even if the NPI doesn't match to an entry in the doc table, or a zipcode can't be matched against the zctas table. 这种特殊的构造将确保即使NPIdoc表中的条目不匹配,或者邮政编码无法与zctas表匹配,也可以表示所referred每个条目。

On the flip side, if there exists more than one entry for an NPI in the doc table, any referred entry that mentions this duplicated NPI will also be duplicated. 另一方面,如果doc表中的一个NPI存在多个条目,则任何提及此重复NPI referred条目也将被复制。

Similarly, if there is more than one entry in zctas for a particular zip code ( zcta ), you would see duplicates of referred rows. 同样,如果特定邮编( zcta )的zctas有多个条目,则将看到referred行的重复项。

That's how LEFT JOIN works, but I figured it was worth putting in the warning, as Provider data is typically full of duplicates against NPI, and there are often duplicate zip codes in zip code lookup lists as some zip codes cross state lines. 这就是LEFT JOIN工作方式,但是我认为值得警告,因为提供者数据通常充满了针对NPI的重复项,并且在邮政编码查找列表中经常存在重复的邮政编码,因为一些邮政编码跨越了状态行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM