首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

快速从sqlserver中选择随机抽样

快速从 SQL Server 中选择随机抽样的方法有很多种,以下是一种使用 TABLESAMPLE 子句的方法。

首先,你需要确保你的 SQL Server 版本支持 TABLESAMPLE 子句。TABLESAMPLE 子句允许你从表中选择一个随机样本,而不是整个表。这是一个示例查询,用于从名为 myTable 的表中选择 10% 的随机样本:

代码语言:sql
复制
SELECT *
FROM myTable
TABLESAMPLE (10 PERCENT)

这将返回 myTable 中大约 10% 的行。请注意,TABLESAMPLE 子句不能保证返回确切的行数,因为它是随机的。

如果你需要更精确的抽样,可以使用 NEWID() 函数和 TOP 子句。以下是一个示例查询,用于从名为 myTable 的表中选择 1000 行的随机样本:

代码语言:sql
复制
SELECT TOP 1000 *
FROM myTable
ORDER BY NEWID()

这将返回 myTable 中 1000 行的随机样本。请注意,NEWID() 函数会为每行生成一个唯一的随机标识符,并按照该标识符对行进行排序。

无论你使用哪种方法,都需要确保你的查询在性能和资源使用方面是有效的。如果你的表非常大,可能需要考虑其他因素,例如分区和索引。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

SAS-如何随心所欲的抽取样本

我们在处理大样本的时候,往往会遇到随机抽样的需求,在SAS中抽样的方法有一个专门的Proc过程步(Proc surveyselect),这个过程步可以简单快速的实现一些随机抽样,有时候我们的随机抽样并不是那么呆版的抽样,这个时候proc surveyselect可能就不那么好用了,比如我们要质检一批数据,每个数据集观测都不一样,需要从每个数据集中随机抽取100条记录,如果不足100条则全部抽取出来...这个如何用proc surveyselect实现呢?反正小编是不会!当然仅仅是这,其实小编还是可以用proc surveyselect过程步做出来的,只是在抽样前获取数据集观测数,进行判断...如果小于指定观测,直接输出结果,如果大于则用抽样过程步进行简单的抽样!

00

Python数据分析(中英对照)·Random Choice 随机选择

通常,当我们使用数字时,偶尔也会使用其他类型的对象,我们希望使用某种类型的随机性。 Often when we’re using numbers, but also,occasionally, with other types of objects,we would like to do some type of randomness. 例如,我们可能想要实现一个简单的随机抽样过程。 For example, we might want to implement a simple random sampling process. 为此,我们可以使用随机模块。 To this end, we can use the random module. 所以,我们的出发点是,再次导入这个模块,random。 So the starting point is, again, to import that module, random. 让我们考虑一个简单的例子,其中列表中包含一组数字,我们希望从这些数字中随机统一选择一个。 Let’s think about a simple example where we have a set of numbers contained in a list,and we would like to pick one of those numbers uniformly at random. 在本例中,我们需要使用的函数是random.choice,在括号内,我们需要一个列表。 The function we need to use in this case is random.choice,and inside parentheses, we need a list. 在这个列表中,我将只输入几个数字——2、44、55和66。 In this list, I’m going to just enter a few numbers– 2, 44, 55, and 66. 然后,当我运行随机选择时,Python会将其中一个数字返回给我。 And then when I run the random choice, Python returns one of these numbers back to me. 如果我重复同一行,我会得到一个不同的答案,因为Python只是随机选取其中一个对象。 If I repeat the same line, I’m going to get a different answer,because, again, Python is just picking one of those objects at random. 关于随机选择方法,需要了解的一个关键点是Python并不关心所使用对象的基本性质 A crucial thing to understand about the random choice method is that Python doesn’t care about the fundamental nature of the objects that 都包含在该列表中。 are contained in that list. 这意味着,不用数字,我也可以从几个字符串中选择一个。 What that means, instead of using numbers,I could also be choosing one out of several strings. 让我们看看这是怎么回事。 So let’s see how that might work. 我要回到我的清单上。 I’m going to go back to my list. 我只想在这里包括三个短字符串。 I’m just going to include three short strings here. 让我们只做“aa”,“bb”和“cc” Let’s just do "aa," "bb," and "cc." 我可以让Python随机选择其中一个。 I can ask Python to pick one of these uniformly at random. 因此Python并不关心这些对象的性质。 So Python doesn’t care about the nature of these objects. 对于任何类型的对象,随机的工作方式都是一样的。 Random works just the same way for any type of object.

03
领券