【问题标题】:TERADATA SQL- how to find a sequence in the dataTERADATA SQL-如何在数据中查找序列
【发布时间】:2020-08-31 03:26:12
【问题描述】:

我有一个包含以下列的表格:account、validity_month、amount。该表包含半年的数据:2019 年 1 月至 2019 年 6 月。对于每个帐户,我都试图在“金额”字段中查找序列——这意味着 6 个月的序列具有相似的金额(关闭在 10% 的范围内)。 如果账户有序列,则ind=1,否则为0。

account    validity_month   amount    
-------   ---------------   --------
123        201901           1000  
123        201901           500 
123        201902           1002 
123        201902           3000  
123        201903           0
123        201903           1050         
123        201904           1020  
123        201905           1020 
123        201905           555 
123        201906           998       

在此示例中,匹配 6 个月的金额相似 (1000,1002,1050,1020,1020,998)。 10%的范围是根据上个月的数值计算出来的。

account    validity_month   amount    
-------   ---------------   --------
124        201901           500  
124        201901           0 
124        201902           530 
124        201903           500
124        201903           2000         
124        201904           2000  
124        201905           60 
124        201905           2100
124        201906           2000       

在此示例中,没有匹配项(3 个月的金额相似,然后 3 个月的金额不同)。 在这种情况下,这是请求的输出:

account    IND    
-------   ------
123          1
124          0

有人可以帮忙吗? 谢谢!

【问题讨论】:

  • 如果序列是100、95、90、85、80、75呢?每个相邻值都在 10% 以内。但它们与平均水平的差异更大。
  • 没关系。根据上个月的值计算的10%范围

标签: sql teradata


【解决方案1】:

如果我假设所有值都需要在彼此的 10% 以内,那么蛮力可能是唯一的方法:

select t1.account,
       (case when t2.account is not null and t3.account is not null and . . .
             then 1 else 0
        end) as flag
from t t1 left join
     t t2
     on t2.account = t1.account and
        t2.amount between t1.amount / 1.1 and t1.amount * 1.1 and
        t2.validity_month = 201902 left join
     t t3
     on t3.account = t1.account and
        t3.amount between t1.amount / 1.1 and t1.amount * 1.1 and
        t3.validity_month = 201903 left join
     . . . 
where t1.validity_month = 201901 
group by t1.account;

【讨论】:

    猜你喜欢
    • 2017-07-19
    • 2021-08-30
    • 2017-07-29
    • 1970-01-01
    • 2015-04-03
    • 2021-07-02
    • 1970-01-01
    • 2016-12-17
    • 2017-07-04
    相关资源
    最近更新 更多