我正在尝试从包含 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/