We are having issues with an online payment gateway system sometimes duplicating transaction entries within minutes of each other.
We want to create a record of these transactions, so we can analyse and correct them.
Single table, as follows. This shows results for only one card number. We would prefer to return only transactions that occurred on the same day, and preferably within 5 seconds of each other as with the last two rows.
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
So far, I have the following, which gets duplicate records for a given card number, but I am not sure how to granularize this even further in order to get records in the same day and preferably within 5 seconds of each other.
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
Could someone give me a hand with this please?
Replace your commented out part of query with the above statement and you should get what you need.