在teradata中生成增量序列号

时间:2018-03-06 04:09:37

标签: sql sequence teradata

我有一个场景,会有2个表。

  1. scratch.data_table

  2. scratch.temp_table

  3. scratch.data_table将以增量方式将其中一个字段作为sequence_id,如4000,4001,4002

    其他表scratch.temp_table将有其他列与scratch.data_table相同但除了sequence_id列

    scratch.temp_table将有5条记录,我想从max {400}获取sequence_id scratch.data_table

    并将其从scratch.temp_table

    的5条记录中插入到相同的表scratch.data_table中

    结果应该是:

    4000 | a | b | c
    4001 | x | y | z
    4002 | s | t | d
    4003 | d | t | f   --> d|t|f is the records from scratch.temp_table and 4003 is the incremental value
    4004 | d | g | h
    4005 | g | t | h
    4006 | y | u | i
    4007 | y | y | t
    

1 个答案:

答案 0 :(得分:1)

select
    coalesce((select max(sequence_id) from data_table),0)
    + row_number() over (order by not_too_skewed_column_eg_PI)
 , t.*
from temp_table as t

或者您保留一个序列表,其中每个表存储当前最大值

相关问题