PostgreSQL 不同的 on、group by、having 合而为一?
我在使用 PostgreSQL 查询时遇到一些问题
我想要的是一个“活动”列表,按距离排序,并且距某个位置的最小距离 [in_lat、in_lng、in_radius] 现在,我正在检索距离(使用 distance_in_km(...) )两次。一次用于订购,一次用于检查活动是否在我的首选半径内。
另外,一个活动可能有多个“Campaignreceiver”条目(接收类型[电子邮件、网站等...]),
并且我只想检索该活动一次。因此,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
I'm am having some trouble with a PostgreSQL query
What I want is a list of "Campaigns", ordered by distance, and within a minimum distance from a location [in_lat, in_lng, in_radius]
Now, I am retrieving the distance (with distance_in_km(...) ) twice. Once for ordering, once for checking if the campaign is within my preferred radius.
Also, It's possible that a campaign has multiple "Campaignreceiver" entries (receivetype [EMAIL, WEBSITE, etc...])
And I want to retrieve the campaign just once. hence the DISTINCT ON.
So the question is, how can I get sometime like "WHERE distance < in_radius" to work.
My guess is adding a GROUP BY and HAVING but I did some tests and didn't get is to work.
ANY HELP IS VERY MUCH APPRECIATED!!!
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AFAIK,只要您使用相同的参数,distance_in_km() 只会被调用一次(每行),因此 WHERE 和 ORDER BY 不会再次评估它。
AFAIK, distance_in_km() will be called only once (per row), as long as you use it with the same arguments, thus WHERE and ORDER BY will not evaluate it again.