SQL数据库。
我必须确定发生的重复交易清单,范围为5-6分钟。
BillId Trans DateTime
A100125 Paid 2018-04-18 11:21:40.873 - Valid Transaction
A100125 Paid 2018-04-18 11:24:40.873 - Duplicate Transaction
A100125 Paid 2018-04-18 11:30:40.873 - Duplicate Transaction
A100125 Paid 2018-04-18 12:30:40.873 - Valid Transaction
我可以为日期范围生成报告。这大约是5-10分钟的平均差异。
答案 0 :(得分:2)
这是
简而言之,case语句中存在一个比较每一行的子查询。这不是最有效的,因为约瑟夫不是sargable
declare @tabel Table (BillId varchar(8000), Trans varchar(50), DateOfSomethingProbablyTheTransaction DateTime)
declare @interval int = 6
insert into @tabel
values ('A100125', 'Paid', '2018-04-18 11:21:40.873'), --valid
('A100125', 'Paid', '2018-04-18 11:24:40.873'), -- Duplicate Transaction
('A100125', 'Paid', '2018-04-18 11:30:40.873'), -- Duplicate Transaction
('A100125', 'Paid', '2018-04-18 12:30:40.873') -- Valid Transaction)
select *,
case when exists(select 1
from @tabel t2
where t1.BillId = t2.BillId
and t1.Trans = t2.Trans
and DATEDIFF(MINUTE, t2.DateOfSomethingProbablyTheTransaction, t1.DateOfSomethingProbablyTheTransaction) <= @interval
and t1.DateOfSomethingProbablyTheTransaction > t2.DateOfSomethingProbablyTheTransaction)
then 'Invalid' else'valid' end as Validity
from @tabel t1
另请注意,如果您希望它看起来更好,因为查询优化器只是在场景后面进行连接,如果trans状态无关紧要,可以将其删除以检查重复项 p>
答案 1 :(得分:1)
我偷了其他人的设置:
但这是一个使用滞后的答案:
declare @tabel Table (BillId varchar(8000), Trans varchar(50), DateOfSomethingProbablyTheTransaction DateTime)
declare @interval int = 6
insert into @tabel
values ('A100125', 'Paid', '2018-04-18 11:21:40.873'), --valid
('A100125', 'Paid', '2018-04-18 11:24:40.873'), -- Duplicate Transaction
('A100125', 'Paid', '2018-04-18 11:30:40.873'), -- Duplicate Transaction
('A100125', 'Paid', '2018-04-18 12:30:40.873') -- Valid Transaction)
(
select BillId, Trans, DateOfSomethingProbablyTheTransaction dt
,lag(DateOfSomethingProbablyTheTransaction,1) over (partition by BillID,Trans order by DateOfSomethingProbablyTheTransaction)
,case when datediff(minute
,lag(DateOfSomethingProbablyTheTransaction,1) over (partition by BillID,Trans order by DateOfSomethingProbablyTheTransaction)
,DateOfSomethingProbablyTheTransaction)<=6
then 'Invalid'
else 'Valid'
end DataCheck
from @tabel
)
结果:
BillId Trans dt (No column name) DataCheck
A100125 Paid 2018-04-18 11:21:40.873 NULL Valid
A100125 Paid 2018-04-18 11:24:40.873 2018-04-18 11:21:40.873 Invalid
A100125 Paid 2018-04-18 11:30:40.873 2018-04-18 11:24:40.873 Invalid
A100125 Paid 2018-04-18 12:30:40.873 2018-04-18 11:30:40.873 Valid
再次使用CTE :(不确定何时开始LAG)
;with base as
(
select BillId, Trans, DateOfSomethingProbablyTheTransaction dt
,rn = row_number() over (partition by BillId, Trans order by DateOfSomethingProbablyTheTransaction)
from @tabel
)
select base.*
,prior.dt
,Test = case when datediff(minute,isnull(prior.dt,'1/1/1900'),base.dt) <=6 then 'Invalid' else 'Valid' end
from base
left join base as [prior] on base.rn-1 = [prior].rn
and base.BillId=[prior].BillId
and base.Trans = [prior].trans
答案 2 :(得分:0)
我不理解你需要什么,但这可能会有所帮助。这使得同一账单id上的交易具有不同的时间,其中时间差小于7分钟。
select *
from Transactions T1
INNER JOIN Transactions T2
ON T1.BillId=T2.BillId
AND T1.DateTime<>T2.DateTime
AND DATEDIFF(MI,T1.DateTime,T2.DateTime)<7
如果我误解了,请告诉我。
答案 3 :(得分:0)
毫无疑问,有更好的方法可以做到这一点。但是,无论如何,我们走了。
SELECT q1.Billid, q1.Trans, q1.TranTime ,
CONVERT(INT, Isnull(Datediff(minute, q2.TranTime, q1.TranTime), 0)) AS [Difference],
CASE
WHEN CONVERT(INT, Isnull(Datediff(minute, q2.TranTime, q1.TranTime), 100)) > 6 THEN 'Valid' -- (diff > 6) || (diff = 0)
ELSE 'Invalid'
END as Validity
FROM (SELECT *,
Row_number() OVER (ORDER BY [Billid], [Trans], TranTime DESC) AS rn
FROM tablename2) q1
LEFT JOIN ((SELECT *,
Row_number() OVER (ORDER BY [Billid], [Trans], TranTime DESC) AS rn
FROM tablename2)) q2
ON q1.rn = q2.rn - 1
ORDER BY TranTime ASC
结果
+---------+-------+-------------------------+------------+----------+
| Billid | Trans | TranTime | Difference | validity |
+---------+-------+-------------------------+------------+----------+
| A100125 | Paid | 2018-04-18 11:21:40.873 | 0 | Valid |
| A100125 | Paid | 2018-04-18 11:24:40.873 | 3 | Invalid |
| A100125 | Paid | 2018-04-18 11:30:40.873 | 6 | Invalid |
| A100125 | Paid | 2018-04-18 12:30:40.873 | 60 | Valid |
| A100125 | Paid | 2018-04-18 12:31:40.873 | 1 | Invalid |
+---------+-------+-------------------------+------------+----------+