3

我们遇到了在线支付网关系统的问题,有时会在几分钟内重复交易条目。

我们想要创建这些交易的记录,以便我们可以分析和更正它们。

单表,如下。这仅显示一个卡号的结果。我们宁愿只返回同一天发生的交易,最好是在 5 秒之内,就像最后两行一样。

txn_authcode   card_number   cardtype   txn_status   txn_value   entryTimeStamp
-------------------------------------------------------------------------------
1491109220     ....0279      Visa       FAILED       20.00       2011-06-24 19:49:00
1491109219     ....0279      Visa       FAILED       20.00       2012-05-28 22:47:57
1491109218     ....0279      Visa       FAILED       20.00       2012-05-28 22:46:39
1491109217     ....0279      Visa       FAILED       20.00       2012-05-28 22:46:35

到目前为止,我有以下内容,它获取给定卡号的重复记录,但我不确定如何进一步细化此记录,以便在同一天获取记录,最好在 5 秒内获取记录。

        select * from(
            select  t1.txn_authcode,t1.txn_status,t1.txn_value,t1.entryTimeStamp
                from    transactions t1
                where 1=1
                and exists
                (select null
                 from   transactions t2
                 where  t1.card_number = t2.card_number
                 and t1.entryTimeStamp <> t2.entryTimeStamp
                 and t2.entryTimeStamp >= '2012-05-01'
                 and t2.entryTimeStamp <= '2012-06-01'
                 --*** AND DATEDIFF ( day , t1.entryTimeStamp , t2.entryTimeStamp ) < 1 
    --(datediff above doesn't work as it can return a single record for a given card, 
--but we only want records that have at least one other transaction record on the same 
--day for the same card)

                 )
                 and t1.entryTimeStamp >= '2012-05-01'
                 and t1.entryTimeStamp <= '2012-06-01'
             )x
        order by card_number,entryTimeStamp desc

有人可以帮我解决这个问题吗?

4

2 回答 2

3
...
AND DATEDIFF ( day , t1.entryTimeStamp , t2.entryTimeStamp ) < 1
AND t1.txn_authcode < t2.txn_authcode
...

用上面的语句替换查询的注释部分,你应该得到你需要的。

于 2012-06-04T22:30:10.977 回答
3

如果您需要间隔几秒钟,请使用

AND DATEDIFF ( ss, t1.entryTimeStamp , t2.entryTimeStamp ) < 5

如果您只需要同一天,请使用

SQL Server 2008 或更高版本

AND CONVERT(date,t1.entryTimeStamp) = convert(date,t2.entryTimeStamp)

SQL Server 2005 或更早版本

and convert(char(10),t1.entryTimeStamp,101) = convert(char(10),t2.entryTimeStamp,101)

如果两者都需要,请使用组合。

于 2012-06-04T22:45:27.383 回答