此查询返回每个用户在一小时内失败的存款次数。它仅过滤 5 个或更多失败的存款。 然而,现在我只需要过滤在 10 分钟内存款 5 笔或更多的用户。 我不知道该怎么做。
with windowed_data as
(
select *,
max(window_count) over (partition by user_id, transaction_window) as max_window_count
from (select
*,
COUNT(*) OVER (PARTITION BY user_id, DATE_TRUNC('hour', created) ORDER BY created) AS window_count,
DATE_TRUNC('hour', created) AS transaction_window
FROM source.payments2_transactions
WHERE state = 'FAILED'
and type = 'DEPOSIT'
and country = 'PE'
and created >= current_date - interval '120 days'
and created < current_date + interval '1 day') wd)
select
user_id,
created,
window_count,
transaction_window,
max_window_count
from windowed_data wd
where max_window_count >= 5
;
请尝试以下操作,我现在修改了查询,以识别在过去 120 天内的任何 10 分钟窗口内经历过 5 次或以上存款交易失败的用户,特别是在秘鲁的存款。它通过首先计算每笔交易的“
transaction_window
”来实现这一点,这将时间戳与最近的 10 分钟区块对齐。
WITH windowed_data AS (
SELECT
*,
COUNT(*) OVER (PARTITION BY user_id, transaction_window) AS window_count,
transaction_window
FROM (
SELECT
user_id,
created,
DATE_TRUNC('minute', created) - (EXTRACT(MINUTE FROM created)::integer % 10) * INTERVAL '1 minute' AS transaction_window,
state
FROM
source.payments2_transactions
WHERE
state = 'FAILED'
AND type = 'DEPOSIT'
AND country = 'PE'
AND created >= CURRENT_DATE - INTERVAL '120 days'
AND created < CURRENT_DATE + INTERVAL '1 day'
) AS subquery
),
aggregated_data AS (
SELECT
user_id,
MAX(window_count) OVER (PARTITION BY user_id, transaction_window) AS max_window_count,
transaction_window
FROM
windowed_data
)
SELECT
user_id,
transaction_window,
max_window_count
FROM
aggregated_data
WHERE
max_window_count >= 5;