如何在 Teradata SQL 中生成自动编号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21982004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:09:06  来源:igfitidea点击:

How to generate AUTOMATIC Number in Teradata SQL

sqlauto-incrementteradata

提问by user2002948

I want to generate AUTOMATIC Number to use TD SQL, for example as follows,

我想生成AUTOMATIC Number来使用TD SQL,例如如下,

CREATE MULTISET TABLE TEST_TABLE
(
  AUTO_NUMBER INT,
  NAME VARCHAR(10)
)
PRIMARY INDEX (AUTO_NUMBER);

INSERT INTO TEST_TABLE
VALUES('TOM');
INSERT INTO TEST_TABLE
VALUES('JIM');
INSERT INTO TEST_TABLE
VALUES('JAN');

SELECT * FROM TEST_TABLE;

The result above will be ,

上面的结果将是,

1 TOM
2 JIM
3 JAN

回答by Lenin Raj Rajasekaran

Create a column with the below syntax:

使用以下语法创建一列:

SEQ_NUM decimal(10,0) NOT NULL GENERATED ALWAYS AS IDENTITY
           (START WITH 1 
            INCREMENT BY 1 
            MINVALUE 1 
            MAXVALUE 2147483647 
            NO CYCLE)

回答by Rony

Usually there is a column in the table which is unique.

通常表中有一列是唯一的。

You can use below technique to add a column in your result set if you dont want to add extra column to your table.

如果您不想向表中添加额外的列,可以使用以下技术在结果集中添加一列。

select RANK() OVER ( ORDER BY ),T.* SEQ from TABLE T;

从表 T 中选择 RANK() OVER ( ORDER BY ),T.* SEQ;

It will give you output like:

它将为您提供如下输出:

1 a xx yy 2 b xx yy 3 c xx yy

1 a xx yy 2 b xx yy 3 c xx yy