[英]TERADATA SQL- how to find a sequence in the data
我有一个包含以下列的表:帐户、有效性月、金额。 该表包含半年的数据: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 个月的金额不同)。 在这种情况下,这是请求的 output:
account IND
------- ------
123 1
124 0
任何人都可以帮忙吗? 谢谢!
如果我假设所有值都需要在彼此的 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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.