我正在创建一个表,并使用query1联合query2将数据插入到该表中。问题是我想将row_number()添加到表中,但是当我将row_number() over()添加到这两个查询中的任何一个时,编号只应用于query1或query2,而不是整个表。
我使用insert query1 union query2将数据插入到表(table_no_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
发布于 2019-04-16 20:04:10
使用子查询:
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;请注意,这也会将union更改为union all。union all更高效;只有当您想要产生删除重复项的开销时,才使用union。
https://stackoverflow.com/questions/55702110
复制相似问题