查找具有相似日期值的行

Find rows with similar date values

我想找到客户,例如,系统错误地注册了订单的重复项。

这很简单,如果 reg_date 完全相同,但我不知道如何在查询中实现它以算作重复,例如,如果事务之间最多有 1 秒的差异。

select * from
(select customer_id, reg_date, count(*) as cnt

 from orders

group by 1,2

) x where cnt > 1

这是示例数据集: https://www.db-fiddle.com/f/m6PhgReSQbVWVZhqe8n4mi/0

目前只有客户的 104 个订单被算作重复订单,因为它 reg_date 是相同的,我也想计算订单 1,2 和 4,5,因为只有 1 秒的差异

尝试以下脚本。这将 return 你 day/customer 明智的重复。

SELECT 
TO_CHAR(reg_date :: DATE, 'dd/mm/yyyy') reg_date,
customer_id, 
count(*) as cnt
FROM orders
GROUP BY 
TO_CHAR(reg_date :: DATE, 'dd/mm/yyyy'),
customer_id
HAVING count(*) >1

demo:db<>fiddle

SELECT
    customer_id,
    reg_date
FROM (
    SELECT
        *,
        reg_date - lag(reg_date) OVER (PARTITION BY customer_id ORDER BY reg_date) <= interval '1 second' as is_duplicate
    FROM
        orders
) s
WHERE is_duplicate

使用lag()window function。它允许查看以前的记录。使用此值,您可以进行 diff 并过滤 diff 时间超过一秒的记录。