Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何在5-10分钟的时间差内获取重复记录?_Sql Server_Sql Server 2008 - Fatal编程技术网

Sql server 如何在5-10分钟的时间差内获取重复记录?

Sql server 如何在5-10分钟的时间差内获取重复记录?,sql-server,sql-server-2008,Sql Server,Sql Server 2008,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 P

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分钟的平均差异。

我无法100%理解您需要什么,但这可能会有所帮助。这将为同一票据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
如果我有误解,请告诉我。

这里有些东西

简而言之,在比较每一行的case语句中有一个exist子查询。这并不是最有效的,因为datediff不是可搜索的

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状态不相关,则可以将其删除以检查重复项

我偷了其他人的设置:

但这里有一个使用滞后的答案:

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
)
再次使用CTE:不确定滞后何时开始

;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

不言而喻,有更好的方法可以做到这一点。但是,不管怎样,我们走吧

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  |
+---------+-------+-------------------------+------------+----------+

那很好。你能分享一些细节吗?像表定义、示例数据和所需输出之类的内容?您是否碰巧有一个主列或自动inc列?我们有一个主键。但是我们没有标识列或自动抄送列,如果您有3行,每行的时间范围为5分钟,该怎么办?您将有多少份副本?延迟时间为2012年及以后。我也期望royalties@SCFi-增加了针对旧式2008R2用户的CTE方法
+---------+-------+-------------------------+------------+----------+
| 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  |
+---------+-------+-------------------------+------------+----------+