简体   繁体   English

如何使用SQL检索Teradata表中最后插入的行

[英]How to retrieve last inserted row in a teradata table using SQL

I have the below multiset teradata table 我有下面的多组Teradata表

CREATE MULTISET TABLE pp_scratch.HADOOP_FPTI_DASHBOARD ,NO FALLBACK ,
     NO BEFORE JOURNAL,
     NO AFTER JOURNAL,
     CHECKSUM = DEFAULT,
     DEFAULT MERGEBLOCKRATIO
     (
      job_status VARCHAR(32) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL,
      current_processing_hr VARCHAR(32) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL,
      no_of_files_moved VARCHAR(10) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL)
PRIMARY INDEX upi_HADOOP_FPTI_DASHBOARD ( current_processing_hr );

When I am trying to insert the values it is not in the order I have inserted. 当我尝试插入值时,它的插入顺序不正确。 If it is in order I have inserted then I will retrieve the value by using order by statement. 如果是按顺序插入的,那么我将使用order by语句检索值。 In this case how to retrieve the last inserted value. 在这种情况下,如何检索最后插入的值。

Sample data 样本数据

在此处输入图片说明

Teradata tables are hash partitoned/sorted, there's no way to get the last row unless current_processing_hr is unique. Teradata表是散列的/已散列的,除非current_processing_hr是唯一的,否则无法获取最后一行

And you data types are wrong: 而且您的数据类型是错误的:

  • change current_processing_hr to a timestamp(2) default current_timestamp(2) , if you don't insert multiple rows in the same 1/100th second you can use this for the last row current_processing_hr更改为timestamp(2) default current_timestamp(2) ,如果您未在同一1/100秒内插入多行,则可以在最后一行使用它
  • change no_of_files_moved to INT no_of_files_moved更改为INT

What you can do here is add a sequence ( auto increment numeric integer ). 您可以在此处添加一个序列(自动递增数字整数)。 For this you would have to alter your table and add a new column ( datatype integer ). 为此,您将不得不更改表并添加一个新列(数据类型integer)。 This column will automatically increment its counter by 1 with every record you insert. 每插入一条记录,此列将自动将其计数器加1。 Now, if you order by that new column, you will be able to get the last row which was inserted into the table. 现在,如果您按该新列进行排序,则将能够获取插入到表中的最后一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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