我們遇到的問題是,在線支付網關係統有時會在幾分鐘內複製交易條目。SQL - 「重複」記錄但日期不同,並且在同一天內
我們想創建這些交易的記錄,所以我們可以分析和糾正它們。
單表如下。這顯示只有一個卡號的結果。我們希望只返回發生在同一天的交易,最好是在最後兩行的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秒內granularize這彼此的。
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
有人能幫我一下嗎?