将记录从一个 Table 随机分配给另一个
Randomly Assign Records From One Table to Another
我有两张桌子。
T1
id,date,item,channel
T2
id,date,item,channel
在 T2 中,id 和 date 列为 NULL。我想将 T1 中的 ID 和日期随机分配给 T2 中的每一行。另外,T2 比 T1 小很多。
有什么办法吗?我在使用 Teradata 13。
我原本是这样想的:
sel count(*) from t2 ;
--507
select *
from (sel a.*, RANDOM(1,507) as r1 from t1) a
inner join (sel b.*, RANDOM(1,507) as r1 from t2) b
on a.r1 = b.r1
问题是我需要自动将 t2 的计数指定为随机数的上限。
根据随机排序分配 ROW_NUMBER 并加入:
select *
from
(
select dt.*, row_number() over (order by t1.r) as rn
from
(
select t1.*, RANDOM(1,1000000) as r from t1
) as dt
) as a
join
(
select dt.*, row_number() over (order by t1.r) as rn
from
(
select t2.*, RANDOM(1,1000000) as r from t2
) as dt
) as b
on a.rn = b.rn
我有两张桌子。
T1
id,date,item,channel
T2
id,date,item,channel
在 T2 中,id 和 date 列为 NULL。我想将 T1 中的 ID 和日期随机分配给 T2 中的每一行。另外,T2 比 T1 小很多。
有什么办法吗?我在使用 Teradata 13。
我原本是这样想的:
sel count(*) from t2 ;
--507
select *
from (sel a.*, RANDOM(1,507) as r1 from t1) a
inner join (sel b.*, RANDOM(1,507) as r1 from t2) b
on a.r1 = b.r1
问题是我需要自动将 t2 的计数指定为随机数的上限。
根据随机排序分配 ROW_NUMBER 并加入:
select *
from
(
select dt.*, row_number() over (order by t1.r) as rn
from
(
select t1.*, RANDOM(1,1000000) as r from t1
) as dt
) as a
join
(
select dt.*, row_number() over (order by t1.r) as rn
from
(
select t2.*, RANDOM(1,1000000) as r from t2
) as dt
) as b
on a.rn = b.rn