可能重复:
如何在 SQL 中随机选择行?
假设——在 Teradata 12 中——我有一个包含 Transaction_ID 和 Store_nbr 的表 TXN。还假设我有一个包含 Store_nbr 和 n_samples 的表 SAMP。
假设我想从 TXN 中抽取 n_samples Transaction_ID 的样本。即,我希望 SAMP 表能够驱动从 TXN 表中获取多少 transaction_id 样本。例如,
TXN
Transaction_ID Store_nbr
1 1
2 1
1 2
2 2
3 2
...和...
SAMP
store_nbr n_samples
1 2
2 2
3 1
那么我如何告诉 SQL(这是 Teradata,顺便说一句):给我 2 个用于存储 1 的 transaction_Id 样本,2 个用于存储 2 的 transaction_Id 样本,以及 1 个用于存储 3 的 transaction_Id 样本?
Teradata 确实有条件样本概念:
select a,b
from foo
sample when a=1 then 5
when a=2 then 10
when a=3 then 7
end
...如果需要,我可以使用 codegen 方法创建(有数千家商店)。
SAMP 表可以随时自由更改。