筛选表里面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 好像不支持索引吧?

...全文
279 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. 我觉得用存储过程写思路和这个差不多吧 。
  • 打赏
  • 举报
回复
看的是懂非懂的,直接上数据。
内容概要:本文围绕“粒子群优化算法驱动的永磁同步电机电流环多参数协同辨识研究”,提出一种基于Simulink仿真实现的高精度参数辨识方法,旨在解决永磁同步电机(PMSM)在高性能电流控制中因关键参数(如电感、电阻、永磁磁链等)不准确或时变所导致的控制性能下降问题。研究构建了PMSM的精确数学模型,并在Simulink环境中搭建完整的电流环控制系统仿真平台,创新性地引入粒子群优化算法(PSO)对多个关键参数进行在线或多工况下的协同辨识。文中详细阐述了PSO算法的核心机制、适应度函数的设计原则(通常基于模型输出与实际响应的误差最小化)、参数初始化与收敛判据,并通过大量仿真实验验证了该方法在不同负载和运行条件下的辨识精度、收敛速度与强鲁棒性,有效提升了电流环的动态响应能力、抗干扰性和整体控制系统的稳定性。; 适合人群:具备电机控制理论、现代智能优化算法基础及MATLAB/Simulink仿真能力,从事电气工程、自动化控制、新能源汽车电驱动系统等领域的高校研究生、科研人员及企业研发工程师。; 使用场景及目标:①应用于高精度伺服驱动、电动汽车电驱系统等对电机参数敏感的高端装备中,实现控制器的精细化标定与自适应;②为智能优化算法在复杂非线性系统参数辨识领域的工程应用提供完整的仿真案例和技术范本;③服务于电机控制系统的故障诊断、性能评估及下一代自整定控制器的研发。; 阅读建议:建议读者结合提供的Simulink模型与MATLAB代码进行动手实践,重点剖析PSO算法与电机模型的耦合机制,深入理解适应度函数的构建逻辑,并可进一步尝试将该方法拓展至温度补偿、磁饱和等非线性因素的联合辨识,或迁移至其他类型的电机(如感应电机)控制中,以深化对数据驱动与模型驱动融合控制策略的理解。

27,579

社区成员

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

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