创建一个新列,对 Teradata 中其他列的信息进行分组
Create a new column, grouping info from other columns in Teradata
有这个:
BEGIN TRANSACTION;
CREATE TABLE MY_TABLE(Salary integer, Name text, Country text);
INSERT INTO MY_TABLE VALUES(10,'Tom', 'Albania');
INSERT INTO MY_TABLE VALUES(20,'Lucy', 'Bulgaria');
INSERT INTO MY_TABLE VALUES(30,'Frank', 'Bulgaria');
INSERT INTO MY_TABLE VALUES(30,'Jane', 'Canada' );
INSERT INTO MY_TABLE VALUES(50,'Robert', 'Canada');
INSERT INTO MY_TABLE VALUES(50,'Robert2', 'Canada');
INSERT INTO MY_TABLE VALUES(100,'Robert2', 'Canada');
INSERT INTO MY_TABLE VALUES(50,'Robert3', 'Canada');
INSERT INTO MY_TABLE VALUES(60,'Robert6', 'Denmark');
COMMIT;
SELECT Salary, Name, Country,Salary || Country AS 'New' FROM MY_TABLE
WHERE Salary < 100
制作这个:
Salary
Name
Country
New
10
Tom
Albania
10Albania
20
Lucy
Bulgaria
20Bulgaria
30
Frank
Bulgaria
30Bulgaria
30
Jane
Canada
30Canada
50
Robert
Canada
50Canada
50
Robert2
Canada
50Canada
50
Robert3
Canada
50Canada
60
Robert6
Denmark
60Denmark
问题:
我想要一个后续数字,而不是 New
中的值,这对所有相等的字符串都是相同的。例如,像这样:
Salary
Name
Country
New
10
Tom
Albania
1
20
Lucy
Bulgaria
2
30
Frank
Bulgaria
3
30
Jane
Canada
4
50
Robert
Canada
5
50
Robert2
Canada
5
50
Robert3
Canada
5
60
Robert6
Denmark
6
数字5是一样的,因为50加拿大在上面的table中重复了3次
你可以尝试使用dense_rank
window功能
SELECT *,dense_rank() OVER(ORDER BY Salary,Country) new
FROM MY_TABLE
WHERE Salary < 100
有这个:
BEGIN TRANSACTION;
CREATE TABLE MY_TABLE(Salary integer, Name text, Country text);
INSERT INTO MY_TABLE VALUES(10,'Tom', 'Albania');
INSERT INTO MY_TABLE VALUES(20,'Lucy', 'Bulgaria');
INSERT INTO MY_TABLE VALUES(30,'Frank', 'Bulgaria');
INSERT INTO MY_TABLE VALUES(30,'Jane', 'Canada' );
INSERT INTO MY_TABLE VALUES(50,'Robert', 'Canada');
INSERT INTO MY_TABLE VALUES(50,'Robert2', 'Canada');
INSERT INTO MY_TABLE VALUES(100,'Robert2', 'Canada');
INSERT INTO MY_TABLE VALUES(50,'Robert3', 'Canada');
INSERT INTO MY_TABLE VALUES(60,'Robert6', 'Denmark');
COMMIT;
SELECT Salary, Name, Country,Salary || Country AS 'New' FROM MY_TABLE
WHERE Salary < 100
制作这个:
Salary | Name | Country | New |
---|---|---|---|
10 | Tom | Albania | 10Albania |
20 | Lucy | Bulgaria | 20Bulgaria |
30 | Frank | Bulgaria | 30Bulgaria |
30 | Jane | Canada | 30Canada |
50 | Robert | Canada | 50Canada |
50 | Robert2 | Canada | 50Canada |
50 | Robert3 | Canada | 50Canada |
60 | Robert6 | Denmark | 60Denmark |
问题:
我想要一个后续数字,而不是 New
中的值,这对所有相等的字符串都是相同的。例如,像这样:
Salary | Name | Country | New |
---|---|---|---|
10 | Tom | Albania | 1 |
20 | Lucy | Bulgaria | 2 |
30 | Frank | Bulgaria | 3 |
30 | Jane | Canada | 4 |
50 | Robert | Canada | 5 |
50 | Robert2 | Canada | 5 |
50 | Robert3 | Canada | 5 |
60 | Robert6 | Denmark | 6 |
数字5是一样的,因为50加拿大在上面的table中重复了3次
你可以尝试使用dense_rank
window功能
SELECT *,dense_rank() OVER(ORDER BY Salary,Country) new
FROM MY_TABLE
WHERE Salary < 100