简体   繁体   English

按结果出现的半径数将结果按ST_DWith排序

[英]Rank order ST_DWithin results by the number of radii a result appears in

I have a table of existing customers and another table of potential customers. 我有一个现有客户表和另一个潜在客户表。 I want to return a list of potential customers rank ordered by the number of radii of existing purchasers that they appear in. 我想返回一个潜在客户的列表,该列表按出现的现有购买者的半径数量排序。

There are many rows in the potential customers table per each existing customer, and the radius around a given existing customer could encompass multiple potential customers. 每个现有客户在潜在客户表中有很多行,并且给定现有客户周围的半径可能包含多个潜在客户。 I want to return a list of potential customers ordered by the count of the existing customer radii that they fall within. 我想返回一个潜在客户列表,该列表按其所属的现有客户半径的数量进行排序。

SELECT pur.contact_id AS purchaser, count(pot.*) AS nearby_potential_customers 
FROM purchasers_geocoded pur, potential_customers_geocoded pot 
WHERE ST_DWithin(pur.geom,pot.geom,1000)
GROUP BY purchaser;

Does anyone have advice on how to proceed? 有人对如何进行有任何建议吗?

EDIT: 编辑:

With some help, I wrote this query, which seems to do the job, but I'm verifying now. 在一些帮助下,我编写了此查询,该查询似乎可以完成工作,但我现在正在验证。

WITH prequalified_leads_table AS (
   SELECT *
   FROM nearby_potential_customers
   WHERE market_val > 80000
   AND   market_val < 120000
   )
, proximate_to_existing AS (
   SELECT pot.prop_id AS prequalified_leads
   FROM purchasers_geocoded pur, prequalified_leads_table pot
   WHERE ST_DWithin(pot.geom,pur.geom,100)
  )
SELECT prequalified_leads, count(prequalified_leads)
FROM proximate_to_existing
GROUP BY prequalified_leads
ORDER BY count(*) DESC;

I want to return a list of potential customers ordered by the count of the existing customer radii that they fall within. 我想返回一个潜在客户列表,该列表按其所属的现有客户半径的数量进行排序。

Your query tried the opposite of your statement, counting potential customers around existing ones. 您的查询尝试使用与语句相反的方法,将现有客户周围的潜在客户计算在内。
Inverting that, and after adding some tweaks: 反过来,并添加一些调整后:

SELECT pot.contact_id AS potential_customer
     , rank() OVER (ORDER BY pur.nearby_customers DESC
                           , pot.contact_id) AS rnk
     , pur.nearby_customers 
FROM   potential_customers_geocoded pot
LEFT   JOIN LATERAL (
   SELECT count(*) AS nearby_customers 
   FROM   purchasers_geocoded pur
   WHERE  ST_DWithin(pur.geom, pot.geom, 1000)
   ) pur ON true
ORDER  BY 2;

I suggest a subquery with LEFT JOIN LATERAL ... ON true to get counts. 我建议使用LEFT JOIN LATERAL ... ON true的子查询来获取计数。 Should make use of the spatial index that you undoubtedly have: 应该利用您无疑拥有的空间索引:

CREATE INDEX ON purchasers_geocoded USING gist (geom);

Thereby retaining rows with 0 nearby customers in the result - your original join style would exclude those. 从而在结果中保留具有0个附近客户的行-您的原始联接样式将排除这些行。 Related: 有关:

Then ORDER BY the resulting nearby_customers in the outer query (not: nearby_potential_customers ). 然后在外部查询中对结果所得的nearby_customers进行ORDER BY (不是: nearby_potential_customers )。

It's not clear whether you want to add an actual rank . 尚不清楚是否要添加实际rank Use the window function rank() if so. 如果是这样,请使用窗口函数rank() I made the rank deterministic while being at it, breaking ties with an additional ORDER BY expression: pot.contact_id . 我当时处于确定性的地位,并与另一个ORDER BY表达式打破联系: pot.contact_id Else, peers are returned in arbitrary order which can change for every execution. 否则,对等体将以任意顺序返回,每次执行都会改变。

ORDER BY 2 is short syntax for "order by the 2nd out column". ORDER BY 2是“按第二个输出列排序”的缩写语法。 See: 看到:

Related: 有关:

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

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