筛选表里面EnCardID不相同,EnTime相差不超过10分钟的记录

十八道胡同 2013-11-21 05:00:19
我有一个ENList表,里面存的都是一些入口记录,每条记录都有VehPlate,EnCardID,EnTime等信息,我现在想找到对于一个VehPlate来说EnCardID不相同,EnTime相差不超过10分钟的记录。

VehPlate 车牌
EnCardID 入口时所使用的卡
EnTime 入口时间

简单之就是找同一个车辆,在间隔不到10分钟的时间内使用多次不同卡进入的记录。

用C#我可以完成此功能,但是考虑到数据比较大,用存储过程可能会降低点执行时间。

我碰到的难点:由于需要对同一个车的相邻记录进行比对,用C#时,用for取i,i+1条记录就可以的,但是SQL 里的for 好像不支持索引吧?

...全文
285 9 打赏 收藏 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
十八道胡同 2013-11-22
  • 打赏
  • 举报
回复
SELECT 
*
FROM 
ENLISTVEHDB201305 a
WHERE 
EXISTS(SELECT 1 FROM ENLISTVEHDB201305  WHERE ENVEHPLATE=a.ENVEHPLATE AND enCardID!=a.enCardID AND timestampdiff(2,char(timestamp(EnTime)-timestamp(a.EnTime)))<=10)
AND
NOT EXISTS(SELECT 1 FROM ENLISTVEHDB201305 WHERE ENVEHPLATE=a.ENVEHPLATE AND enCardID=a.enCardID AND EnTime<a.EnTime) order by ENVEHPLATE
这个是我用db2写的一个sql,timestampdiff(2,char(timestamp(EnTime)-timestamp(a.EnTime)))<=10 这个是比对小于10s的,记录表里有2条这个数据,但是结果里却有第一条数据,难道我哪里写错了? 'WJ0812110' '2013-05-01 17:01:00.000000' 1098937908 'WJ0812110' '2013-05-01 17:00:03.000000' 0
十八道胡同 2013-11-22
  • 打赏
  • 举报
回复
我给的数据格式不符合SQl Server的要求,数据库内的数据是SQL Server的datetime类型的。
十八道胡同 2013-11-22
  • 打赏
  • 举报
回复
引用 4 楼 fredrickhu 的回复:
----------------------------------------------------------------
-- Author  :fredrickhu(小F,向高手学习)
-- Date    :2013-11-21 17:25:02
-- Verstion:
--      Microsoft SQL Server 2012 - 11.0.2100.60 (X64) 
--	Feb 10 2012 19:39:15 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go 
create table [tb]([vehPlate] int,[enCardID] varchar(3),[EnTime] varchar(16))
insert [tb]
select 111111,'c1','2013-11-21 17:11' union all
select 111111,'c1','2013-11-21 17:15' union all
select 111111,'c2','2013-11-21 17:15' union all
select 222222,'c3','2013-11-21 17:13' union all
select 222222,'c4','2013-11-22 17:13' union all
select 222222,'c5','2013-11-21 17:09' union all
select 333333,'c9','2013-11-22 17:08' union all
select 444444,'c10','2013-11-22 17:09'
--------------开始查询--------------------------
SELECT 
*
FROM 
TB a
WHERE 
EXISTS(SELECT 1 FROM TB  WHERE vehPlate=a.vehPlate AND enCardID<>a.enCardID AND DATEDIFF(mi,EnTime,a.EnTime)<=10)
AND
NOT EXISTS(SELECT 1 FROM TB WHERE vehPlate=a.vehPlate AND enCardID=a.enCardID AND EnTime<a.EnTime)
----------------结果----------------------------
/* vehPlate    enCardID EnTime
----------- -------- ----------------
111111      c1       2013-11-21 17:11
111111      c2       2013-11-21 17:15
222222      c3       2013-11-21 17:13
222222      c5       2013-11-21 17:09

(4 行受影响)

*/
引用 6 楼 yupeigu 的回复:
是这样吗:

create table ENList
(VehPlate varchar(10),EnCardID varchar(10),EnTime varchar(20))

insert into ENList
 select '111111','c1','2013-11-21-17.11' union all
 select '111111','c1','2013-11-21-17.15' union all
 select '111111','c2','2013-11-21-17.15' union all
 select '222222','c3','2013-11-21-17.13' union all
 select '222222','c4','2013-11-22-17.13' union all
 select '222222','c5','2013-11-21-17.09' union all
 select '333333','c9','2013-11-22-17.08' union all
 select '444444','c10','2013-11-22-17.09'



-- 建目标表
create table EnListException
(VehPlate varchar(10),EnCardID varchar(10),EnTime varchar(20))


;with t 
as
(
select *,
       replace(stuff(EnTime,len(EnTime)-CHARINDEX('-',reverse(EnTime))+1,1,' '),
               '.',':') as t_EnTime,
       
       row_number() over(partition by VehPlate 
                             order by EnTime desc) as rownum
from ENList
)


insert into EnListException
select t1.VehPlate,t1.EnCardID,t1.EnTime
from t t1
inner join t t2
        on t1.vehPlate = t2.VehPlate
           and t1.rownum = t2.rownum + 1
           and DATEDIFF(MINUTE,t2.t_enTime,t1.t_EnTime) < 10
 order by t1.VehPlate,t1.EnCardID          
 

select * from EnListException       
/*
VehPlate	EnCardID	EnTime
111111	c1	2013-11-21-17.11
111111	c2	2013-11-21-17.15
222222	c3	2013-11-21-17.13
222222	c5	2013-11-21-17.09
*/    
引用 5 楼 ap0405140 的回复:

-- 建测试表
create table ENList
(VehPlate varchar(10),EnCardID varchar(10),EnTime varchar(20))

insert into ENList
 select '111111','c1','2013-11-21-17.11' union all
 select '111111','c1','2013-11-21-17.15' union all
 select '111111','c2','2013-11-21-17.15' union all
 select '222222','c3','2013-11-21-17.13' union all
 select '222222','c4','2013-11-22-17.13' union all
 select '222222','c5','2013-11-21-17.09' union all
 select '333333','c9','2013-11-22-17.08' union all
 select '444444','c10','2013-11-22-17.09'

-- 建目标表
create table EnListException
(VehPlate varchar(10),EnCardID varchar(10),EnTime varchar(20))


with t as
(select VehPlate,EnCardID,EnTime,
        cast(left(EnTime,10)+' '+replace(right(EnTime,5),'.',':') as datetime) 'EnTime2'
 from ENList),
u as
(select VehPlate,EnCardID,EnTime,EnTime2,
        row_number() over(partition by VehPlate order by EnTime2 desc) 'rn'
 from t)
insert into EnListException(VehPlate,EnCardID,EnTime)
select a.VehPlate,a.EnCardID,a.EnTime 
 from u a
 left join u b on a.VehPlate=b.VehPlate and a.rn=b.rn+1
 where datediff(m,a.EnTime2,b.EnTime2)<10
 order by a.VehPlate,a.EnCardID

-- 结果
select * from EnListException

/*
VehPlate   EnCardID   EnTime
---------- ---------- --------------------
111111     c1         2013-11-21-17.11
111111     c2         2013-11-21-17.15
222222     c3         2013-11-21-17.13
222222     c5         2013-11-21-17.09

(4 row(s) affected)
*/
谢谢各位的热心回复。
LongRui888 2013-11-21
  • 打赏
  • 举报
回复
是这样吗:

create table ENList
(VehPlate varchar(10),EnCardID varchar(10),EnTime varchar(20))

insert into ENList
 select '111111','c1','2013-11-21-17.11' union all
 select '111111','c1','2013-11-21-17.15' union all
 select '111111','c2','2013-11-21-17.15' union all
 select '222222','c3','2013-11-21-17.13' union all
 select '222222','c4','2013-11-22-17.13' union all
 select '222222','c5','2013-11-21-17.09' union all
 select '333333','c9','2013-11-22-17.08' union all
 select '444444','c10','2013-11-22-17.09'



-- 建目标表
create table EnListException
(VehPlate varchar(10),EnCardID varchar(10),EnTime varchar(20))


;with t 
as
(
select *,
       replace(stuff(EnTime,len(EnTime)-CHARINDEX('-',reverse(EnTime))+1,1,' '),
               '.',':') as t_EnTime,
       
       row_number() over(partition by VehPlate 
                             order by EnTime desc) as rownum
from ENList
)


insert into EnListException
select t1.VehPlate,t1.EnCardID,t1.EnTime
from t t1
inner join t t2
        on t1.vehPlate = t2.VehPlate
           and t1.rownum = t2.rownum + 1
           and DATEDIFF(MINUTE,t2.t_enTime,t1.t_EnTime) < 10
 order by t1.VehPlate,t1.EnCardID          
 

select * from EnListException       
/*
VehPlate	EnCardID	EnTime
111111	c1	2013-11-21-17.11
111111	c2	2013-11-21-17.15
222222	c3	2013-11-21-17.13
222222	c5	2013-11-21-17.09
*/    
唐诗三百首 2013-11-21
  • 打赏
  • 举报
回复

-- 建测试表
create table ENList
(VehPlate varchar(10),EnCardID varchar(10),EnTime varchar(20))

insert into ENList
 select '111111','c1','2013-11-21-17.11' union all
 select '111111','c1','2013-11-21-17.15' union all
 select '111111','c2','2013-11-21-17.15' union all
 select '222222','c3','2013-11-21-17.13' union all
 select '222222','c4','2013-11-22-17.13' union all
 select '222222','c5','2013-11-21-17.09' union all
 select '333333','c9','2013-11-22-17.08' union all
 select '444444','c10','2013-11-22-17.09'

-- 建目标表
create table EnListException
(VehPlate varchar(10),EnCardID varchar(10),EnTime varchar(20))


with t as
(select VehPlate,EnCardID,EnTime,
        cast(left(EnTime,10)+' '+replace(right(EnTime,5),'.',':') as datetime) 'EnTime2'
 from ENList),
u as
(select VehPlate,EnCardID,EnTime,EnTime2,
        row_number() over(partition by VehPlate order by EnTime2 desc) 'rn'
 from t)
insert into EnListException(VehPlate,EnCardID,EnTime)
select a.VehPlate,a.EnCardID,a.EnTime 
 from u a
 left join u b on a.VehPlate=b.VehPlate and a.rn=b.rn+1
 where datediff(m,a.EnTime2,b.EnTime2)<10
 order by a.VehPlate,a.EnCardID

-- 结果
select * from EnListException

/*
VehPlate   EnCardID   EnTime
---------- ---------- --------------------
111111     c1         2013-11-21-17.11
111111     c2         2013-11-21-17.15
222222     c3         2013-11-21-17.13
222222     c5         2013-11-21-17.09

(4 row(s) affected)
*/
--小F-- 2013-11-21
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author  :fredrickhu(小F,向高手学习)
-- Date    :2013-11-21 17:25:02
-- Verstion:
--      Microsoft SQL Server 2012 - 11.0.2100.60 (X64) 
--	Feb 10 2012 19:39:15 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go 
create table [tb]([vehPlate] int,[enCardID] varchar(3),[EnTime] varchar(16))
insert [tb]
select 111111,'c1','2013-11-21 17:11' union all
select 111111,'c1','2013-11-21 17:15' union all
select 111111,'c2','2013-11-21 17:15' union all
select 222222,'c3','2013-11-21 17:13' union all
select 222222,'c4','2013-11-22 17:13' union all
select 222222,'c5','2013-11-21 17:09' union all
select 333333,'c9','2013-11-22 17:08' union all
select 444444,'c10','2013-11-22 17:09'
--------------开始查询--------------------------
SELECT 
*
FROM 
TB a
WHERE 
EXISTS(SELECT 1 FROM TB  WHERE vehPlate=a.vehPlate AND enCardID<>a.enCardID AND DATEDIFF(mi,EnTime,a.EnTime)<=10)
AND
NOT EXISTS(SELECT 1 FROM TB WHERE vehPlate=a.vehPlate AND enCardID=a.enCardID AND EnTime<a.EnTime)
----------------结果----------------------------
/* vehPlate    enCardID EnTime
----------- -------- ----------------
111111      c1       2013-11-21 17:11
111111      c2       2013-11-21 17:15
222222      c3       2013-11-21 17:13
222222      c5       2013-11-21 17:09

(4 行受影响)

*/
十八道胡同 2013-11-21
  • 打赏
  • 举报
回复
vehPlate enCardID EnTime 111111 c1 2013-11-21-17.11 111111 c1 2013-11-21-17.15 111111 c2 2013-11-21-17.15 222222 c3 2013-11-21-17.13 222222 c4 2013-11-22-17.13 222222 c5 2013-11-21-17.09 333333 c9 2013-11-22-17.08 444444 c10 2013-11-22-17.09 假设表是这样的,那么最后EnListException 表里就有4条记录 111111 c1 2013-11-21-17.11 111111 c2 2013-11-21-17.15 222222 c3 2013-11-21-17.13 222222 c5 2013-11-21-17.09
引用 1 楼 SQL 的回复:
看的是懂非懂的,直接上数据。
十八道胡同 2013-11-21
  • 打赏
  • 举报
回复
我的 C#的思路是这样的: 1,找到所有的vehPlate。 2,对于每个车牌,找到他的所有入口流水且按照entime降序,然后用for依次比对相邻的2个记录,看是不是enCardID不相同 且EnTime 相差不到10分钟的记录,这些记录先放dataTable里,等所有车牌走一遍了,在批量插入新的表EnListException. 我觉得用存储过程写思路和这个差不多吧 。
  • 打赏
  • 举报
回复
看的是懂非懂的,直接上数据。
内容概要:本文围绕【复现IEEE二区文献】基于状态空间表示与组件连接法的构网型逆变器统一小信号建模框架研究,提出了一种适用于多控制策略构网型逆变器的小信号建模方法。该方法结合状态空间建模理论与组件连接法(Component Connection Method),实现了对复杂逆变器系统动态特性的精确描述,尤其适用于分析系统在不同运行工况下的稳定性。通过Matlab代码实现,构建了包含控制环路、滤波器、锁相环等关键模块的完整小信号模型,并利用特征值分析与模态参与度评估系统稳定性,解决了传统建模方法在面对多时间尺度、强耦合非线性控制结构时建模困难、精度不足的问题。研究还探讨了关键控制参数对系统稳定边界的影响,为构网型逆变器的参数整定与优化设计提供了理论依据和技术支撑。; 适合人群:具备电力电子、自动控制理论基础,从事新能源发电、微电网、电力系统稳定性研究的研究生、科研人员及工程技术人员,尤其适合致力于高水平期刊论文复现与仿真实践的科研工作者。; 使用场景及目标:① 掌握构网型逆变器统一小信号建模的核心方法,复现IEEE二区高水平文献成果;② 理解状态空间法与组件连接法在复杂电力电子系统建模中的集成应用;③ 利用Matlab进行特征值分析与稳定性判据研究,支撑科研仿真与论文写作;④ 为多逆变器并联系统、微电网稳定性分析等课题提供建模工具与技术路线参考。; 阅读建议:建议读者结合Matlab代码与文中建模流程逐步调试,重点关注各子模块状态方程的推导与系统整体矩阵组装过程,深入理解控制参数对系统极点分布的影响规律,建议配合其他稳定性分析案例进行对比学习以加深理解。
内容概要:WiFiSupply斯普莱WFS7000XB52AX5G是一款高性能、工业级的5G全网通+双频WIFI6无线AP基站,支持5G与工业大功率Wi-Fi链路无缝切换与带宽聚合,具备高达2974Mbps的双频带宽和1000Mbps以上的实际吞吐量。设备采用MIMO 4T4R架构,支持OFDMA、AI智能FastRoaming等先进技术,实现低至20ms的漫游切换延迟和超远传输距离(点对点桥接可达150公里以上)。产品具备IP68防水、-45~85℃宽温运行、多重看门狗、防浪涌、防静电等工业级防护设计,适用于复杂恶劣环境。支持胖瘦AP模式、多种认证与管理方式(含云平台、API接口),并集成丰富的网络安全与QoS功能,适用于智慧物流、工业4.0、巡检机器人、无人机、车载船舶等多种场景。; 适合人群:从事工业物联网、智能制造、自动化物流、智能交通等领域的网络工程师、系统集成商、设备制造商及技术决策人员。; 使用场景及目标:①实现移动设备(如AGV/AMR/巡检机器人)在高速移动中的零丢包、低延迟无线漫游;②在无固定网络覆盖区域构建高带宽、高可靠的远距离无线通信链路;③为5G与工业Wi-Fi双链路备份与聚合提供一体化解决方案,保障关键业务连续性;④在复杂电磁环境下实现稳定、安全的数据传输与远程控制。; 阅读建议:本产品技术参数详尽,建议结合具体应用场景重点关注其无线性能、漫游技术、供电方式与安装选项,并参考官方提供的管理平台与API接口文档进行系统集成与远程运维规划。

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧