日期:2014-05-18 浏览次数:20524 次
CREATE TABLE [dbo].[DepartureTime]( [departureTime] [varchar](50) NULL, [departureTime2] [datetime] NULL, [carNum] [varchar](50) NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[RecordTable]( [carId] [int] NULL, [carNum] [varchar](50) NULL, [GpsTime] [datetime] NULL, [inOut] [int] NULL, [regionId] [int] NULL, [RegionName] [varchar](50) NULL ) ON [PRIMARY] GO
CREATE TABLE [dbo].[DepartureTime]( [departureTime] [varchar](50) NULL, [departureTime2] [datetime] NULL, [carNum] [varchar](50) NULL ) ON [PRIMARY] insert into DepartureTime select '08:15','2011-11-28 08:15:00','闽B02929' insert into DepartureTime select '09:15','2011-11-28 09:15:00','闽B02929' insert into DepartureTime select '10:15','2011-11-28 10:15:00','闽B02929' insert into DepartureTime select '11:15','2011-11-28 11:15:00','闽B02929' insert into DepartureTime select '12:15','2011-11-28 12:15:00','闽B02929' CREATE TABLE [dbo].[RecordTable]( [carId] [int] NULL, [carNum] [varchar](50) NULL, [GpsTime] [datetime] NULL, [inOut] [int] NULL, [regionId] [int] NULL, [RegionName] [varchar](50) NULL ) ON [PRIMARY] insert into RecordTable select 9517,'闽B02929','2011-11-28 08:10:10',1,2728,'钟楼站' insert into RecordTable select 9517,'闽B02929','2011-11-28 08:15:10',0,2728,'钟楼站' insert into RecordTable select 9517,'闽B02929','2011-11-28 09:10:10',1,2751,'水部门兜站' insert into RecordTable select 9517,'闽B02929','2011-11-28 09:16:10',0,2751,'水部门兜站' insert into RecordTable select 9517,'闽B02929','2011-11-28 10:10:10',1,2728,'钟楼站' insert into RecordTable select 9517,'闽B02929','2011-11-28 10:11:10',0,2728,'钟楼站' insert into RecordTable select 9517,'闽B02929','2011-11-28 11:10:10',1,2751,'水部门兜站' insert into RecordTable select 9517,'闽B02929','2011-11-28 11:14:10',0,2751,'水部门兜站' insert into RecordTable select 9517,'闽B02929','2011-11-28 12:10:10',1,2728,'钟楼站' insert into RecordTable select 9517,'闽B02929','2011-11-28 12:15:10',0,2728,'钟楼站' go select a.departureTime,a.carNum,b.GpsTime as InTime,c.GpsTime as OutTime,b.RegionName from [DepartureTime] a inner join [RecordTable] b on a.carNum=b.carNum and datediff(mi,a.departureTime2,b.GpsTime) between -10 and 10 inner join [RecordTable] c on c.carNum=b.carNum and c.RegionName=b.RegionName and datediff(mi,b.GpsTime,c.GpsTime)between 0 and 10 where b.inOut=1 and c.inOut=0 /* departureTime carNum InTime OutTime RegionName --------------- --------------- ------------------------ ----------------------- ----------------------- -------- 08:15 闽B02929 2011-11-28 08:10:10.000 2011-11-28 08:15:10.000 钟楼站 09:15 闽B02929 2011-11-28 09:10:10.000 2011-11-28 09:16:10.000 水部门兜站 10:15 闽B02929 2011-11-28 10:10:10.000 2011-11-28 10:11:10.000 钟楼站 11:15 闽B02929 2011-11-28 11:10:10.000 2011-11-28 11:14:10.000 水部门兜站 12:15 闽B02929 2011-11-28 12:10:10.000 2011-11-28 12:15:10.000 钟楼站 (5 行受影响) */ go --drop table RecordTable,DepartureTi