在 SQL (Sybase Server IQ) 中选择随机子集 [英] Selecting a Random Subset in SQL (Sybase Server IQ)

查看:44
本文介绍了在 SQL (Sybase Server IQ) 中选择随机子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 rand() 或 newid() 选择记录的随机子集,但每次运行以下命令时,我总是得到相同的结果:

I'm trying to select a random subset of records using either rand() or newid(), but any time I run the following, I always get the same results back:

SELECT *,rand() as rid
INTO #mytable
FROM othertable

select top 10 * from #mytable order by rid

我已经尝试了所有的变体,但是当我像上面那样做时,sybase server iq 似乎总是返回相同的 rand() 值.当我尝试做

I've tried every variation of this, but it seems that sybase server iq always returns the same rand() value when I do it like the above. When I try to do

SELECT * FROM othertable order by newid()

我收到一条错误消息,说在 order by 中使用 newid() 是非法的.

I get an error saying it's illegal to use newid() in the order by.

有什么建议吗?

推荐答案

在 Sybase T-SQL 中,NEWID() 系统函数生成人类可读的全局唯一 ID.在目标表上发出 SELECT 语句时,在 ORDER BY 子句中包含 NEWID() 系统函数.

In Sybase T-SQL, The NEWID() system function generates human-readable, globally unique IDs. Include the NEWID() system function in the ORDER BY clause when issuing the SELECT statement on the target table.

用法:

SELECT top 10 id from TABLE order by NEWID();

这篇关于在 SQL (Sybase Server IQ) 中选择随机子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆