我在使用 PostgreSQL 查询时遇到了一些问题
我想要的是一个“活动”列表,按距离排序,并且在距位置 [in_lat, in_lng, in_radius] 的最小距离内现在,我正在检索距离(使用 distance_in_km(...) )两次。一次用于订购,一次用于检查活动是否在我的首选范围内。
此外,一个活动可能有多个“Campaignreceiver”条目(receivetype [EMAIL、WEBSITE 等...])
而且我只想检索一次广告系列。因此 DISTINCT ON。
所以问题是,我怎样才能得到像“WHERE distance < in_radius”这样的工作。我的猜测是添加一个 GROUP BY 和 HAVING 但我做了一些测试并没有得到工作。
很感谢任何形式的帮助!!!
SELECT DISTINCT ON (c.campaign_uuid, distance)
c.campaign_uuid,
prl.lat,
prl.long,
distance_in_km(prl.lat, prl.long, in_lat, in_lng) AS distance
FROM
usercampaignrelations AS ucr
LEFT JOIN
campaignreceivers AS cr
ON
ucr.usercampaign_uuid = cr.usercampaign_uuid
LEFT JOIN
campaigntargetgrouprelations AS ctg
ON
cr.campaigntargetgroup_uuid = ctg.campaigntargetgroup_uuid
LEFT JOIN
campaigns AS c
ON
ucr.campaign_uuid = c.campaign_uuid
LEFT JOIN
companycampaignrelations AS cc
ON
c.campaign_uuid = cc.campaign_uuid
LEFT JOIN
pointradiuslocations AS prl
ON
c.location_uuid = prl.location_uuid
WHERE
ucr.user_uuid = in_user_uuid
AND
(cr.status = 'SENT' OR cr.status = 'RETRIEVED')
AND
distance_in_km(prl.lat, prl.long, in_latitude, in_longtitude) < in_radius
ORDER BY
distance
LIMIT
in_limit