简体   繁体   English

插入记录时Teradata中的计数器

[英]Counters in Teradata while inserting records

I am trying to insert records in a table in the below format 我正在尝试以以下格式在表中插入记录

Name              Amount       Date       Counter    
A                  100        Jan 1          1
A                  100        Jan2           1
A                  200        Jan 10         2
A                  300        Mar 30         3
B                   50        Jan 7          1
C                   20        Jan 7          1

Could someone tell me the sql for generating the value for the Counter field . 有人可以告诉我用于生成Counter字段值的sql吗? The counter value should increment whenever the amount changes and reset when the name changes. 每当数量更改时,计数器值应增加,而名称更改时,计数器值应重置。

What you need is a DENSE_RANK function. 您需要的是DENSE_RANK函数。 Unfortunately it's not natively implemented before TD14.10, but it can be written using nested OLAP-functions: 不幸的是,它不是在TD14.10之前本地实现的,但是可以使用嵌套的OLAP函数编写:

SELECT
   Name
   ,Amount
   ,date_col
   ,SUM(flag)
    OVER (PARTITION BY Name
          ORDER BY date_col
          ROWS UNBOUNDED PRECEDING) AS "DENSE_RANK"
FROM
 (
   SELECT
      Name
      ,Amount
      ,date_col
      ,CASE
          WHEN Amount = MIN(Amount)
                        OVER (PARTITION BY Name 
                              ORDER BY date_col
                              ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING)
          THEN 0 
          ELSE 1
       END AS flag
   FROM dropme
 ) AS dt;

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

相关问题 Teradata自动增量列以不同范围为起点,同时在不同时间段插入记录 - Teradata auto-increment column taking different ranges as starting point while inserting records at different time periods 插入记录时出现Android SQLite错误 - Android SQLite errors while inserting records 当插入记录时无法访问远程磁盘上的文件时会发生什么情况? - What happens when a file on a remote disk is not reachable while inserting records? 在插入记录时,sqlite表列的默认值 - default value for the sqlite table columns, while inserting records 精确丢失而批量使用SqlBulkCopy将记录插入SQL - Precision Lost while Bulk Inserting records into SQL using SqlBulkCopy 在Teradata中加载700亿条记录 - load 70 Billion records in teradata 通过循环将数据从datagridview插入到sql表时出错 - Error while inserting records from datagridview into sql table by looping 在mysql表中插入数据时忽略所有空记录 - Ignoring all null records while inserting data in mysql table 在teradata中按日期对一组记录进行分组 - group a set of records by date in teradata 在将记录插入到SQL Server时生成带有条件的自定义ID - generate custom id with conditions while inserting records to sql server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM