如何在Redshift中创建行号?

时间:2014-06-26 16:49:59

标签: sql postgresql amazon-redshift

我想使用select语句创建一个表,并添加一个带行号的列

所以我想写一些类似的东西:

create table schema.table_w_rownumbers as 
select 
    identity(1, 1) as nrum,
    foo.*
from schema.initial_table as foo;

2 个答案:

答案 0 :(得分:3)

这对我有用!

create table schema.table_w_rownumbers as 
select 
  row_number() over (partition by 1) as nrum,
  foo.*
from schema.initial_table as foo;

答案 1 :(得分:0)

试试这段代码。确保将order by id更改为需要订购的任何内容。

Create Table schema.table_w_rownumbers
AS
(
select row_number() over (order by id) as nrum, * schema.initial_table
);