我有一个包含[Account_No,Transaction_Amount,Transaction_Date和Transaction_Time]列的表。我想选择交易金额超过2,000,000 /的交易账户和10分钟内的交易时间"一天内。对此有什么疑问。请帮忙。 我正在使用Teradata。
此致
Khawar
答案 0 :(得分:1)
一种方式是自联接(因为Teradata没有实现RANGE OVER
语法),如下所示:
SELECT t1.Account_No,
t1.Transaction_Date,
t1.Transaction_Time,
Sum(t2.Transaction_Amount) AS sumamt
FROM tab AS t1
JOIN tab AS t2
ON t1.Account_No = t2.Account_No
-- same day within the next ten minutes of a transaction
AND t2.Transaction_Date = t1.Transaction_Date
AND t2.Transaction_Time BETWEEN t1.Transaction_Time
AND t1.Transaction_Time + INTERVAL '10' MINUTE
GROUP BY 1,2,3
HAVING sumamt >= 2000000 -- more than 2 mio
QUALIFY
Row_Number() -- only one row per account/day
Over (PARTITION BY t1.Account_No, t1.Transaction_Date
ORDER BY t1.Transaction_Time) = 1