【问题标题】:Teradata SQL -Min Max transaction dates from RowsTeradata SQL - 来自行的最小最大事务日期
【发布时间】:2021-01-01 12:17:49
【问题描述】:

尝试了 Qualify row_Number () 和 Qualify Min & Max 函数,但仍然无法获取交易日期范围。见下面的数据结构

以下输出需要帮助

提前谢谢你

【问题讨论】:

  • 合并行的规则是什么?每 ??? 的连续日期

标签: sql teradata window-functions gaps-and-islands


【解决方案1】:

您需要先找到连续日期的组。有几种方法可以做到这一点,在您的情况下,最好的方法是将一个序列与另一个序列进行比较,其中有间隙:

with cte as
 (
   select t.*
      -- consecutive numbers = sequence without gaps
     ,row_number()
      over (partition by location, cust#, cust_type -- ??
            order by transaction_date) as rn
      -- consecutive numbers as long as there's no missing date = sequence with gaps
     ,(transaction_date - date '0001-01-01') as rn2

      -- assign a common (but meaningless) value to consecutive dates,
      -- value changes when there's a gap
     ,rn2 - rn as grp
   from tab as t
 )
select location, cust#, cust_type -- ??
  ,min(transaction_date), max(transaction_date)
  ,min(amount), max(amount)
from cte
 -- add the calculated "grp" to GROUP BY 
group by location, cust#, cust_type, grp

用于 PARTITION BY/GROUP BY 的列取决于您的规则。

【讨论】:

  • 完成并以惊人的方式解释。谢谢你,先生!!!它给了我确切的输出。
猜你喜欢
  • 1970-01-01
  • 2015-03-12
  • 2021-07-24
  • 1970-01-01
  • 1970-01-01
  • 2014-07-26
  • 2017-04-29
  • 2012-07-16
  • 1970-01-01
相关资源
最近更新 更多