行数()over()continuous over union select

xbp102n0  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(313)

我正在创建一个表,并使用query1联合query2将数据插入到该表中。问题是,我想向表中添加行\u number(),但是当我向任一查询中添加行\u number()over()时,编号只适用于query1或query2,而不适用于整个表。
我用insert query1 union query2将数据插入到表(table\u no\u serial)中,然后创建第二个这样的表

insert into table_w_serial select row_number() over(), * from table_no_serial;

有没有可能第一次就搞定?

insert into table purchase_table 
select row_number() over(), w.ts, w.tail, w.event, w.action, w.msg, w.tags 
from table1 w 
where 
w.action = 'stop'
union 
select row_number() over(), t.ts, t.tail, t.event, t.action, t.msg, t.tags 
from table2 t
where 
f.action = 'stop';

我想要这样的工作。
我想编写一个代码,其中结果表(endtable)将是第一个查询和第二个查询的并集,并将在两个查询中包含一个常量行号,以便如果query1返回50个结果,query2返回40个结果。结束表的行号为1-90

z2acfund

z2acfund1#

使用子查询:

insert into table purchase_table ( . . . ) -- include column names here
    select row_number() over (), ts, tail, event, action, msg, tags
    from ((select w.ts, w.tail, w.event, w.action, w.msg, w.tags 
           from table1 w 
           where w.action = 'stop'
          ) union all 
          (select w.ts, w.tail, w.event, w.action, w.msg, w.tags 
           from table2 w
           where f.action = 'stop'
          )
         ) w;

请注意,这也会发生变化 unionunion all . union all 效率更高;只使用 union 如果您想产生删除重复项的开销。

相关问题