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

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

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

Create table TableX as
select * from TableA
union
select * from TableB limit (10000 - count(*) from TableA);
lrl1mhuk

lrl1mhuk1#

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

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

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

insert into tableX (col1, col2)
select col1, col2
from (
    select col1, col2, 't1' which from table1
    union all 
    select col1, col2, 't2' from table2
) t
order by which, random()
limit 10000
xkrw2x1b

xkrw2x1b2#

with inparms as (
  select 10000 as target_rows
), acount as (
  select count(*) as acount, inparms.target_rows 
    from tablea
   cross join inparms
), btag as (
  select b.*, 'tableb' as tabsource, 
         row_number() over (order by random()) as rnum
    from tableb
)
select a.*, 'tablea', row_number() over (order by 1) as rnum
  from tablea
union all
select b.*
  from btag b
  join acount a on b.rnum <= a.target_rows - a.acount
;

相关问题