简体   繁体   中英

create primary key ROW_NUMBER() over function sql teradata

I want to create a primary key in my select statement. I read that I can use ROW_NUMBER() over function. But as it is going to be primary key, i don't have any columns for over or partition by . I tried using just select row_number() as PK but that throws error [3706] syntax error: expected something between ( and as keyword .

how could I resolve the issue?

You would need an over clause. I'm not sure if the order by is optional in Teradata (I don't have a version on hand):

row_number() over ()
row_number() over (order by <some column here>)

Are you trying to create a auto generated number which you can then use as a primary index for good distribution across the AMPs in Teradata nodes (which you refer to as PK in select statement) ?

If so, and if you dont want to use the IDENTITY COLUMN data type to do that for you (pros and cons exist), then you could generate such a auto number to be used as PI in Teardata by simply using a csum function. (Mind you, your target table must not be too large ie more than a few hundred thousands to a million)

SELECT 
   mx.max_id + csum(1,1) as PI_column
  ,src.columnABC
from 
  source_table src
cross join 
  (SELECT max(id) as max_id from target_table) as mx
group by 1,2
order by 1;

This will generate a new PI/PK/Unique ID column to be used for PI with good distribution for every unique combination of ColumnABC.

Hope this helps.

If my "if" statement at the beginning was not true, then please explain further what are you trying to do and i will be happy to help you with that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM