基于另一个表的计数从表中红移示例

yvgpqqbh  于 2021-08-01  发布在  Java
关注(0)|答案(2)|浏览(372)

我有一个3000行的表格(可以是小于10000的任何数字)。我需要创建10000行的tablex。所以我需要从tableb(以及add-in-tablea)中随机选择10000-(tablea中的行数)来创建tablex。有什么想法吗?像这样的事情(显然行不通):

  1. Create table TableX as
  2. select * from TableA
  3. union
  4. select * from TableB limit (10000 - count(*) from TableA);
lrl1mhuk

lrl1mhuk1#

你可以用 union all 和窗口功能。您没有列出表列,所以我假设 col1 以及 col2 :

  1. insert into tableX (col1, col2)
  2. select col1, col2 from table1
  3. union all
  4. select t2.col1, t2.col2
  5. from (select t2.*, row_number() over(order by random()) from table2 t2) t2
  6. inner join (select count(*) cnt from table1) t1 on t2.rn <= 10000 - t1.cnt

中的第一个查询 union all 从中选择所有行 table1 . 第二个查询将随机行号分配给 table2 ,然后根据需要选择尽可能多的行以达到 10000 .
实际上,从两个表中选择所有行可能更简单,然后 order by 以及 limit 在外部查询中:

  1. insert into tableX (col1, col2)
  2. select col1, col2
  3. from (
  4. select col1, col2, 't1' which from table1
  5. union all
  6. select col1, col2, 't2' from table2
  7. ) t
  8. order by which, random()
  9. limit 10000
展开查看全部
xkrw2x1b

xkrw2x1b2#

  1. with inparms as (
  2. select 10000 as target_rows
  3. ), acount as (
  4. select count(*) as acount, inparms.target_rows
  5. from tablea
  6. cross join inparms
  7. ), btag as (
  8. select b.*, 'tableb' as tabsource,
  9. row_number() over (order by random()) as rnum
  10. from tableb
  11. )
  12. select a.*, 'tablea', row_number() over (order by 1) as rnum
  13. from tablea
  14. union all
  15. select b.*
  16. from btag b
  17. join acount a on b.rnum <= a.target_rows - a.acount
  18. ;

相关问题