/*交易记录挂车牌*/ /*把POSID和车牌一一对应的先匹配 没配上的交易条数65411*/ select * into [temp].[dbo].[jy623_plate1] from (select a.*,b.[车牌] as platenum from [klb].[dbo].[qsjy_0623] a left join (select distinct [POS机编号],[车牌] from [temp].[dbo].[posplate623] where [POS机编号] in ( SELECT distinct [POS机编号] FROM [temp].[dbo].[posplate623] group by [POS机编号] having count(distinct [车牌]) = 1)) b on a.[tjrlposid] = b.[POS机编号]) e /*监控数据中司售卡和车牌一一对应的 匹配车牌 没配上的交易条数7430*/ select * into temp1 from (SELECT * FROM [temp].[dbo].[jy623_plate1] where [platenum] is null ) a ---65411 select * into temp2 from (SELECT a.* , b.[line_name_gj],b.[line_code_jtk] FROM [klb].[dbo].[monitor$] a left join [temp].[dbo].[linematch] b on a.线路 = b.[line_name_gj])b --8210 添加了jtk线路代码的监控数据temp2 select * into [temp].[dbo].[jy623_plate2] from ( select a.tjrlcardno,a.tjrlposid,a.tjrlrdate,a.tjrlrtime,a.[tjrlsdate] ,a.[tjrlcdkind] ,a.[tjrlamt],a.[tjrlstatid] ,a.[tjrlunitid],a.[tjrlorgamt], a.[tjrlposcardid],b.[牌照号] as platenum from temp1 a left join ( select distinct [司售卡编码],[线路],line_code_jtk,[牌照号] from temp2 where [司售卡编码] in (select [司售卡编码] from temp2 where [司售卡编码] != '' group by [司售卡编码],[线路] having count(distinct [牌照号]) = 1)) b on a.tjrlposcardid = b.司售卡编码) a ----考虑有的司机会乱刷线路卡,只用司售卡编码做关联 select * into temp3 from (SELECT * FROM [temp].[dbo].[jy623_plate2] where [platenum] is null ) a ---7530 /*根据交易时间从监控数据中捞车牌补全 又补了1539条*/ UPDATE temp2 SET [实际出场时间] = [计划出场时间] where [实际出场时间] = '' UPDATE temp2 SET [实际出场时间] = substring([实际出场时间],12,2)+substring([实际出场时间],15,2)+substring([实际出场时间],18,2) select * into temp5 from ( select *, cast(e.tjrlrtime as numeric) - cast(e.实际出场时间 as numeric) as delta from ( select a.tjrlcardno,a.tjrlposid,a.tjrlrdate,a.tjrlrtime,a.[tjrlsdate] ,a.[tjrlcdkind] ,a.[tjrlamt],a.[tjrlstatid] ,a.[tjrlunitid],a.[tjrlorgamt], a.[tjrlposcardid],b.实际出场时间,b.牌照号 from (select * from temp3) a left join (select * from temp2 where [司售卡编码] != '' ) b on a.[tjrlposcardid] = b.司售卡编码 )e where e.[实际出场时间] is not null) f ----3222 select * into temp6 from ( select a.tjrlcardno,a.tjrlposid,a.tjrlrdate,a.tjrlrtime,a.[tjrlsdate] ,a.[tjrlcdkind] ,a.[tjrlamt],a.[tjrlstatid] ,a.[tjrlunitid],a.[tjrlorgamt], a.[tjrlposcardid],a.牌照号 as platenum from ( select b.*,ROW_NUMBER() over (partition by tjrlcardno,tjrlposcardid order by delta) as rn from (select * from temp5 where delta > 0) b) a where rn = 1 )g --1593 根据实际出场时间筛选又配了一部分 /*把三次匹配的表整合*/ select * into [temp].[dbo].[jy623_plate3] from ( select distinct c.* , d.platenum as platenum2 from ( select distinct a.*, b.platenum as platenum1 from (select * from temp0) a left join (select * from [temp].[dbo].[jy623_plate2]) b on a.tjrlcardno = b.tjrlcardno and a.tjrlrtime = b.tjrlrtime) c left join (select * from temp6) d on c.tjrlcardno = d.tjrlcardno and c.tjrlrtime = d.tjrlrtime) g -----887667 update [temp].[dbo].[jy623_plate3] set platenum = platenum1 where platenum is null and platenum1 is not null update [temp].[dbo].[jy623_plate3] set platenum = platenum2 where platenum is null and platenum2 is not null /*最终生成的挂车牌交易记录[temp].[dbo].[jy623_platematch]*/ select * into [temp].[dbo].[jy623_platematch] from ( select distinct a.tjrlcardno,a.tjrlposid,a.tjrlrdate,a.tjrlrtime,a.[tjrlsdate] ,a.[tjrlcdkind] ,a.[tjrlamt],a.[tjrlstatid] ,a.[tjrlunitid],a.[tjrlorgamt], a.[tjrlposcardid],a.platenum from [temp].[dbo].[jy623_plate3] a) b /*匹配结果*/ select * from [temp].[dbo].[jy623_platematch] where platenum is null ---5834 select count(distinct tjrlposid) from [temp].[dbo].[jy623_platematch] where platenum is null --70 select count(distinct tjrlposid) from [temp].[dbo].[jy623_platematch] ---5453 /*电子路单及交易数据时间处理*/ delete from [temp].[dbo].[dzld623] where sxx is null update [temp].[dbo].[dzld623] set SJFCSJ = SUBSTRING(SJFCSJ,4,1)+SUBSTRING(SJFCSJ,1,2)+SUBSTRING(SJFCSJ,11,2)+ SUBSTRING(SJFCSJ,14,2)+SUBSTRING(SJFCSJ,17,2) UPDATE [temp].[dbo].[dzld623] SET SJDDSJ = SUBSTRING(SJDDSJ,4,1)+SUBSTRING(SJDDSJ,1,2)+SUBSTRING(SJDDSJ,11,2)+ SUBSTRING(SJDDSJ,14,2)+SUBSTRING(SJDDSJ,17,2) update [temp].[dbo].[jy623_platematch] set tjrlrtime = substring(tjrlrdate,6,3)+tjrlrtime /*错位填补*/ ---select * into temp21 from(select * from [temp].[dbo].[dzld623] where fczdmc not like '%场' and ddzdmc not like '%场')a SELECT * INTO temp21 FROM (SELECT a.* , lag(SJDDSJ, 1, 0) over(PARTITION BY CPH ORDER BY SJFCSJ) changefc FROM [temp].[dbo].[dzld623] a) C ---64292 update temp21 set changefc = SJFCSJ-10000 where changefc = '0' ----5373 /*匹配*/ select * into temp22 from ( select distinct a.*,b.XLMC,b.sxx,b.SJFCSJ,b.FCZDMC, b.SJDDSJ,b.DDZDMC,b.changefc from (select distinct * from [temp].[dbo].[jy623_platematch])a left join (select distinct * from TEMP21) b ON a.platenum = b.CPH where a.tjrlrtime >= b.changefc and a.tjrlrtime < b.SJDDSJ) c ---742555 /*配上方向的全量交易数据*/ select * into [temp].[dbo].[jy623_sxxmatch] from ( select * from ( select distinct a.*, b.XLMC,b.sxx,b.SJFCSJ,b.FCZDMC, b.SJDDSJ,b.DDZDMC from (select distinct * from [temp].[dbo].[jy623_platematch]) a left join (select distinct * from temp22) b on a.tjrlcardno = b.tjrlcardno and a.tjrlrtime = b.tjrlrtime and a.tjrlposid = b.tjrlposid and a.tjrlcdkind= b.tjrlcdkind and a.tjrlamt=b.tjrlamt) c group by c.tjrlcardno,c.tjrlposid,c.tjrlrdate,c.tjrlrtime,c.DDZDMC,c.FCZDMC,c.platenum,c.SJDDSJ,c.SJFCSJ,c.SXX,c.tjrlamt,c.tjrlcdkind, c.tjrlorgamt,c.tjrlposcardid,c.tjrlsdate,c.tjrlstatid,c.tjrlunitid,c.XLMC)d -----887667 /*检查为什么有的没有配上*/ --因为有的交易记录没有配上车牌 select count(*) from [temp].[dbo].[jy623_sxxmatch] where sxx is null and platenum is null ---5834 --交易记录中的车牌在电子路单中找不到 select * from [temp].[dbo].[jy623_sxxmatch] where sxx is null and platenum in ( select a.platenum from (select distinct platenum from [temp].[dbo].[jy623_sxxmatch] where sxx is null and platenum is not null) a left join (select distinct cph from [temp].[dbo].[dzld623]) b on a.platenum = b.cph where b.cph is null) ---137834 --交易记录时间匹配不上 --存在车牌、pos、司售卡不一致 /*以上情况对应的电子路单记录*/ select * from [temp].[dbo].[dzld623] where cph in( select distinct platenum from [temp].[dbo].[jy623_sxxmatch] where sxx is null and platenum not in ( select a.platenum from (select distinct platenum from [temp].[dbo].[jy623_sxxmatch] where sxx is null and platenum is not null) a left join (select distinct cph from [temp].[dbo].[dzld623]) b on a.platenum = b.cph where b.cph is null) ) order by cph,sjfcsj /*以上情况对应的交易记录*/ select * from [temp].[dbo].[jy623_sxxmatch] where sxx is null and platenum not in ( select a.platenum from (select distinct platenum from [temp].[dbo].[jy623_sxxmatch] where sxx is null and platenum is not null) a left join (select distinct cph from [temp].[dbo].[dzld623]) b on a.platenum = b.cph where b.cph is null) order by platenum,tjrlrtime /*-----------------挂站点------------------------*/ select * into [temp].[dbo].[623dlz] from ( SELECT distinct * FROM [temp2].[dbo].[dlz1] union SELECT distinct * FROM [temp2].[dbo].[dlz2] union SELECT distinct * FROM [temp2].[dbo].[dlz3]) a ---2105897 ---把行车方向不等于0/1的记录删除 delete from [temp].[dbo].[623dlz] where direction not in (0,1) --668 ---GPS是断点采集,因此将到达时间前推5s,扩大时间切片范围 select * into temp31 from ( select a.*,(case when a.arrive=0 then DATEADD(ss,-5,a.gpstime) else a.gpstime end) as gpstime_change from temp31 a) b --2105229 ---首发站状态为离开的记录,对离开时间进行更新,避免在首发站提前上车刷卡的记录无法匹配的清况 update temp31 set gpstime_change = DATEADD(mi,-10,gpstime) where [LEVELS] = 1 and arrive = 1 ---65752 update temp31 set arrive = 0 where [LEVELS] = 1 and arrive = 1 ---65752 ---按到站时间做切片,只保留arrive =0 select * into temp32 from (select * from temp32 where arrive = 0) a ---1139816 ---对时间格式进行统一 select * into temp33 from (select a.*, substring(a.gpsdate,4,1)+substring(a.gpsdate,1,2)+substring(CONVERT(CHAR(8),a.gpstime_change,108),1,2)+substring(CONVERT(CHAR(8),a.gpstime_change,108),4,2)+substring(CONVERT(CHAR(8),a.gpstime_change,108),7,2) as ddsj from temp32 a) b ---对时间进行错位补充 SELECT * INTO temp34 FROM (SELECT a.* , lead(ddsj, 1, 0) over(PARTITION BY vehicleid ORDER BY ddSJ) changeddsj FROM temp33 a) C ---1139816 select * into temp35 from( select a.*, substring(a.gpsdate,4,1)+substring(a.gpsdate,1,2)+substring(CONVERT(CHAR(8),a.temp,108),1,2)+substring(CONVERT(CHAR(8),a.temp,108),4,2)+substring(CONVERT(CHAR(8),a.temp,108),7,2) as temp2 from (select b.*, DATEADD(mi,5,gpstime_change) as temp from temp34 b) a)c ---1139816 update temp35 set changeddsj = temp2 where changeddsj = '0' ---5354 update temp35 set changeddsj = substring(changeddsj,1,2)+'4'+substring(changeddsj,4,9) where ddsj > changeddsj update temp35 set DIRECTION = '下行' where direction = '1' update temp35 set DIRECTION = '上行' where direction = '0' ---匹配 select * into temp36 from (select distinct [VEHICLEID],[VEHICLENUMBERING] ,[ROADSORT] ,[GPSDATE] ,[GPSTIME] ,[LEVELS] ,[DIRECTION] ,[ARRIVE] ,[SOURCE] ,[STOPREPORTINGTYPE] ,[SAVEDATE],[gpstime_change] ,[ddsj] ,[changeddsj] from temp35) a select * into temp37 from ( select a.* , b.roadsort,b.gpstime,b.levels from (SELECT * FROM [temp].[dbo].[jy623_sxxmatch] ) a left join (select * from temp36) b on a.platenum = b.vehicleid and a.sxx = b.direction where a.tjrlrtime >= b.ddsj and a.tjrlrtime < b.changeddsj ) c ---719082 //718969 /*配上站点的全量交易数据*/ select * into [temp].[dbo].[jy623_stationmatch] from ( select distinct * from ( select distinct a.*, b.roadsort,b.gpstime,b.levels from (select distinct * from [temp].[dbo].[jy623_sxxmatch]) a left join (select distinct * from temp37) b on a.tjrlcardno = b.tjrlcardno and a.tjrlrtime = b.tjrlrtime and a.tjrlposid = b.tjrlposid and a.tjrlcdkind= b.tjrlcdkind and a.tjrlamt=b.tjrlamt) c)d ---------------------------------------- select * from ( select a.platenum , b.vehicleid from (select distinct platenum from [temp].[dbo].[jy623_stationmatch] where sxx is not null and roadsort is null) a left join (select distinct vehicleid from [temp].[dbo].[623dlz])b on a.platenum = b.VEHICLEID) c where c.vehicleid is null select * from [temp].[dbo].[jy623_stationmatch] where sxx is not null and roadsort is null select count(*) from [temp].[dbo].[jy623_sxxmatch] where sxx is not null select distinct platenum from [temp].[dbo].[jy623_stationmatch] where sxx is not null and roadsort is null select * into temp555 from (select * from [temp].[dbo].[jy623_stationmatch] where roadsort = '55') a select * into temp556 from ( select a.*,b.[站点名称] from (select * from temp555) a left join (SELECT * FROM [temp2].[dbo].[Sheet1$]) b on a.sxx = b.[空] and a.levels = b.[站级]) d select distinct [站点名称] ,sxx, count(*) as num from temp556 group by [站点名称],sxx order by num desc select sum()* from temp556