0

I have a table full of transactions as follows

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

Hour is 24-hour. I want to have a SQL query which returns any transactions that happen on the same card at the same location in a +-5 minute time window. I don't have an issue with the GROUP BY statement or anything, I just can't figure out the logic behind the 5 minute time window.

What I've done so far is create a MinutesSinceMidnight field = 60 * Hour + Minute to get a constant time scale. Then I created RoundMin = Round(MinutesSinceMidnight/5)*5 and CeilingMin. I then checked for duplicates using either of the two values. However, this creates inconsistent time frames. Minute = 53 will not match to Minute = 50 on either scale, but will match to Minute = 57, for example.

Can someone help me with the logic needed?

4

1 回答 1

1

为@Mikhail +1,

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)
于 2013-07-11T10:54:47.413 回答