我有一个交易表,我想从时间差为2小时的表中提取这些交易,并在两个不同的国家/地区进行交易。 我写了以下查询,但问题是它正在获取那些国家/地区相同的交易。
查询是:
SELECT DISTINCT b.*
FROM TRANSACTION_TABLE b, TRANSACTION_TABLE a
WHERE
b.CARD IN (SELECT b.CARD
FROM TRANSACTION_TABLE b, TRANSACTION_TABLE a
WHERE b.TYPE_TXN IN ('21')
AND b.RESPONSE_TXN='00'
AND b.DATETIME_TXN BETWEEN DATEADD(hh,-2,GETDATE()) AND GETDATE()
AND b.CARD=a.CARD
AND b.COUNTRY<>a.COUNTRY
GROUP BY B.CARD
HAVING COUNT(B.CARD)>1 )
AND b.TYPE_TXN in ('21')
AND b.RESPONSE_TXN='00'
AND b.DATETIME_TXN BETWEEN DDATEADD(hh,-2,GETDATE()) AND GETDATE()
AND b.CARD=a.CARD
AND b.COUNTRY<>a.COUNTRY
请指导。
由于
答案 0 :(得分:0)
看起来你应该这样做:
select A.*
from Transaction_Table A,
Transaction_Table B
where -- both transaction should have the same CARD
(A.Card = B.Card) and
-- ... But different countries
-- In order to prevent duplicates > instead of <>
(A.Country > B.Country) and
-- Time difference btw A and B should be less than 2 hours
-- NB! DMBS dependent! (Oracle version implemented)
((Abs(A.DateTime_Txn - B.DateTime_Txn) < 2 / 24) and
-- Other transaction filters
(A.Type_TXN in ('21')) and
(B.Type_TXN in ('21')) and
(A.Response_TXN = '00') and
(B.Response_TXN = '00')