sql - 从雪花 SQL 中随机选择

标签 sql snowflake-cloud-data-platform

我正在尝试从包含 9700 万行的数据库中选择 1,000 个随机行。我正在使用以下代码:

SELECT *
FROM "DB"."SCHEMA"."TABLE"
ORDER BY                                         
   RAND()                                      LIMIT 1000

我尝试了这段代码,但收到一条错误消息,指出“SQL 编译错误:未知函数 RAND”。在 Snowflake 中是否有更好的方法来做到这一点?我担心这段代码只能在 MySQL 中运行。

最佳答案

使用SAMPLE子句:

Returns a subset of rows sampled randomly from the specified table. The following sampling methods are supported:

  • Sample a fraction of a table, with a specified probability for including a given row. The number of rows returned depends on the size of the table and the requested probability. A seed can be specified to make the sampling deterministic.

  • Sample a fixed, specified number of rows. The exact number of specified rows is returned unless the table contains fewer rows.

SELECT *
FROM "DB"."SCHEMA"."TABLE"
SAMPLE (1000 ROWS);

关于sql - 从雪花 SQL 中随机选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71040642/

相关文章:

snowflake-cloud-data-platform - Snowflake 中对非常大的表进行删除操作的最佳方法是什么?

snowflake-cloud-data-platform - 尝试通过 Snowflake PUT 命令加载阶段中的本地文件,但命令显示为灰色

sql - 雪花任务正在执行,但在查询历史记录中找不到它们

mysql - 如何将开始日期和结束日期与其他开始日期和结束日期分开?

sql - 如何使用 OR 组合多个范围

c# - 可移植 C# 数据库

MySQL 查询以查找每周唯一新访问者的数量

mysql - LEFT JOIN 后填充缺失值的最佳方法?

mysql - 在 MySQL 表之间移动行

sql - Snowflake SQL group-by 的行为有所不同,具体取决于列是按位置引用还是按别名引用