SQL Server rand() 聚合
问题:坐标纬度/经度表。 两行可能具有相同的坐标。 我们想要一个返回一组具有唯一坐标的行的查询(在返回的集合内)。 请注意,distinct
不可用,因为我需要返回 id 列,根据定义,该列是不同的。 这种工作方式(@maxcount
是我们需要的行数,intid
是唯一的 int id 列):
select top (@maxcount) max(intid)
from Documents d
group by d.geoLng, d.geoLat
不幸的是,它总是为给定坐标返回相同的行,这对我的使用来说有点遗憾。 如果我们有一个 rand()
聚合,我们就可以使用 rand()
聚合来代替 max()
...请注意,您不能使用 max()
> 使用 newid()
创建的 guid。
有任何想法吗? (如果您有兴趣,这里有更多背景信息:http://www .itu.dk/~friism/blog/?p=121)
更新:完整解决方案此处
Problem: a table of coordinate lat/lngs. Two rows can potentially have the same coordinate. We want a query that returns a set of rows with unique coordinates (within the returned set). Note that distinct
is not usable because I need to return the id column which is, by definition, distinct. This sort of works (@maxcount
is the number of rows we need, intid
is a unique int id column):
select top (@maxcount) max(intid)
from Documents d
group by d.geoLng, d.geoLat
It will always return the same row for a given coordinate unfortunately, which is bit of a shame for my use. If only we had a rand()
aggregate we could use instead of max()
... Note that you can't use max()
with guids created by newid()
.
Any ideas?
(there's some more background here, if you're interested: http://www.itu.dk/~friism/blog/?p=121)
UPDATE: Full solution here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您也许可以将 CTE 与跨纬度和经度的 ROW_NUMBER 函数一起使用,然后使用 rand() 对此进行处理。 类似于:
这将始终返回第一组纬度和经度,但我无法使顺序随机。 也许有人可以继续采用这种方法。 不过,它会在匹配的纬度和经度组合中为您提供随机行。
如果以后有更多时间,我会尝试绕过最后一个障碍。
You might be able to use a CTE for this with the ROW_NUMBER function across lat and long and then use rand() against that. Something like:
This will always return the first sets of lat and lngs and I haven't been able to make the order random. Maybe someone can continue on with this approach. It will give you a random row within the matching lat and lng combinations though.
If I have more time later I'll try to get around that last obstacle.
这对你不起作用?
this doesn't work for you?
您从哪里得知 DISTINCT 仅适用于一列? 无论如何,您也可以使用 GROUP BY 子句。
Where did you get the idea that DISTINCT only works on one column? Anyway, you could also use a GROUP BY clause.