SQL Server rand() 聚合

发布于 2024-07-10 15:07:04 字数 782 浏览 11 评论 0原文

问题:坐标纬度/经度表。 两行可能具有相同的坐标。 我们想要一个返回一组具有唯一坐标的行的查询(在返回的集合内)。 请注意,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请别遗忘我 2024-07-17 15:07:04

您也许可以将 CTE 与跨纬度和经度的 ROW_NUMBER 函数一起使用,然后使用 rand() 对此进行处理。 类似于:

WITH cte AS
(
    SELECT
        intID,
        ROW_NUMBER() OVER
            (
                PARTITION BY geoLat, geoLng
                ORDER BY NEWID()
            ) AS row_num,
        COUNT(intID) OVER (PARTITION BY geoLat, geoLng) AS TotalCount
    FROM
        dbo.Documents
)
SELECT TOP (@maxcount)
    intID, RAND(intID)
FROM
    cte
WHERE
    row_num = 1 + FLOOR(RAND() * TotalCount)

这将始终返回第一组纬度和经度,但我无法使顺序随机。 也许有人可以继续采用这种方法。 不过,它会在匹配的纬度和经度组合中为您提供随机行。

如果以后有更多时间,我会尝试绕过最后一个障碍。

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:

WITH cte AS
(
    SELECT
        intID,
        ROW_NUMBER() OVER
            (
                PARTITION BY geoLat, geoLng
                ORDER BY NEWID()
            ) AS row_num,
        COUNT(intID) OVER (PARTITION BY geoLat, geoLng) AS TotalCount
    FROM
        dbo.Documents
)
SELECT TOP (@maxcount)
    intID, RAND(intID)
FROM
    cte
WHERE
    row_num = 1 + FLOOR(RAND() * TotalCount)

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.

黑凤梨 2024-07-17 15:07:04

这对你不起作用?

select top (@maxcount) *
from 
(
    select max(intid) as id from Documents d group by d.geoLng, d.geoLat
) t 
order by newid()

this doesn't work for you?

select top (@maxcount) *
from 
(
    select max(intid) as id from Documents d group by d.geoLng, d.geoLat
) t 
order by newid()
荒芜了季节 2024-07-17 15:07:04

您从哪里得知 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文