2013-07-10 76 views
0

的I內有一個完整的記錄,表如下確定是否其他值的範圍內的電流值

Tbl_Transactions 
ID  Date  Hour  Minute  Location  CardID 
1  20130101 1   1   a    123456 

小時是24小時。我想要一個SQL查詢,它返回在+ -5分鐘時間窗口中的同一位置上發生的同一卡片上的所有交易。我沒有GROUP BY語句或任何問題,我只是無法弄清5分鐘時間窗口背後的邏輯。

我到目前爲止所做的是創建一個MinutesSinceMidnight字段= 60 *小時+分鐘來獲得一個恆定的時間比例。然後我創建了RoundMin = Round(MinutesSinceMidnight/5)* 5和CeilingMin。然後我使用這兩個值中的任何一個來檢查重複項。但是,這會造成不一致的時間框架。例如,分鐘= 53將不匹配Minute = 50,但會匹配Minute = 57。

有人可以幫助我需要的邏輯?

+0

? – muratgu

+0

爲什麼不將日期,小時和分鐘轉換爲單個日期時間並使用此值而不是三個不同的字段? – Mikhail

+0

@muratgu對不起,「檢查重複」是一個糟糕的選擇。我想在當前記錄的時間範圍內查找在同一位置重複使用卡的實例 – user2395059

回答

1

1爲@Mikhail,

sqlfiddle的例子。

模式:

create table Tbl_Transactions (
    ID int not null 
    ,Date varchar(8) not null 
    ,Hour int 
    ,Minute int 
    ,Location nvarchar(100) 
    ,CardID int 
) 

insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (1,'20130101',1,1,'a',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (2,'20130101',0,2,'a',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (3,'20130201',1,1,'a',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (4,'20130101',20,10,'a',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (5,'20130203',1,1,'a',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (6,'20130101',1,11,'a',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (7,'20130102',0,1,'a',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (8,'20121231',23,59,'a',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (9,'20121231',23,50,'a',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (10,'20130101',1,3,'a',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (11,'20130101',1,5,'a',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (12,'20130301',3,1,'a',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (13,'20130401',1,1,'a',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (14,'20130101',1,1,'b',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (15,'20130101',1,2,'b',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (16,'20130201',1,1,'b',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (17,'20130101',20,10,'b',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (18,'20130203',1,1,'b',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (19,'20130101',1,11,'b',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (20,'20130102',0,1,'b',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (21,'20121231',23,59,'b',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (22,'20121231',23,50,'b',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (23,'20130101',1,3,'b',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (24,'20130101',1,5,'b',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (25,'20130301',1,1,'b',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (26,'20130401',1,1,'b',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (27,'20130101',1,1,'a',123456) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (28,'20130101',0,2,'a',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (29,'20130201',1,1,'a',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (30,'20130101',20,10,'a',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (31,'20130203',1,1,'a',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (32,'20130101',1,1,'a',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (33,'20130102',1,1,'a',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (34,'20121231',23,59,'a',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (35,'20121231',23,50,'a',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (36,'20130101',1,3,'a',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (37,'20130101',1,5,'a',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (38,'20130301',1,1,'a',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (39,'20130401',1,1,'a',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (40,'20130101',1,1,'b',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (41,'20130101',1,2,'b',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (42,'20130201',1,1,'b',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (43,'20130101',20,10,'b',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (44,'20130203',1,1,'b',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (45,'20130101',1,1,'b',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (46,'20130102',1,1,'b',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (47,'20121231',23,59,'b',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (48,'20121231',23,50,'b',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (49,'20130101',1,3,'b',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (50,'20130101',1,5,'b',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (51,'20130301',1,1,'b',123457) 
insert Tbl_Transactions (ID,Date,Hour,Minute,Location,CardID) values (52,'20130401',1,1,'b',123457) 

查詢

declare @picktime datetime= cast('2013/01/01 00:01:00' as datetime) 


SELECT 
    *, 
    cast( 
     Left(Date,4) 
     + '/' 
     + Substring(Date,5,2) 
     + '/' 
     + Right(Date,2) 
     + ' ' 
     + Cast(Hour as varchar) 
     + ':' 
     + Cast(Minute as varchar) 
     + ':00' as datetime) 
    , Dateadd(mi, -5,@picktime), Dateadd(mi, 5, @picktime) 

FROM 
    Tbl_Transactions 
where 
    (
    cast( 
     Left(Date,4) 
     + '/' 
     + Substring(Date,5,2) 
     + '/' 
     + Right(Date,2) 
     + ' ' 
     + Cast(Hour as varchar) 
     + ':' 
     + Cast(Minute as varchar) 
     + ':00' as datetime) 
    between 
     Dateadd(mi, -5,@picktime) 
     and 
     Dateadd(mi, 5, @picktime) 
    ) 
    and 
    (CardID = 123456) 
要尋找一個範圍,然後你`檢查duplicates`