我有一张表格,内容如下
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将与分钟= 50匹配,但是匹配分钟= 57,例如。
有人可以帮我提供所需的逻辑吗?
答案 0 :(得分:1)
+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)