Teradata SQL-行中的最小最大交易日期

问题描述

尝试使用qualify row_Number()和Qualify Min&Max函数,但仍无法获取交易日期范围。请参阅下面的数据结构

enter image description here

需要以下输出的帮助

enter image description here

提前谢谢

解决方法

您需要先找到连续日期的组。有几种方法可以做到这一点,对于您而言,最好的方法是将一个序列与另一个有缺口的序列进行比较:

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_type -- ??,min(transaction_date),max(transaction_date),min(amount),max(amount)
from cte
 -- add the calculated "grp" to GROUP BY 
group by location,cust_type,grp

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