100分求: 怎样去掉表中一个时间的中间的数据?

micrsoft3 2006-10-30 04:46:13
如下表

Table1
-----------------------------------
id name 时间
1 李 2006-10-10 09:00:00
2 李 2006-10-10 10:00:00
3 李 2006-10-10 18:00:00
4 李 2006-10-11 09:00:00
5 李 2006-10-11 18:00:00

上面的是一个考勤记录下,本来是上班打一次卡,下班打一次卡,可是如果有人打了三次以上的卡,则如何删除 中间时段的记录呢?

...全文
173 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
micrsoft3 2006-10-30
  • 打赏
  • 举报
回复
谢谢楼上的兄弟们!

问题解决了~!

分不够,dawugui(潇洒老乌龟) 来这里接分(不回答问题也接)

http://community.csdn.net/Expert/topic/5120/5120235.xml?temp=.3487512
dawugui 2006-10-30
  • 打赏
  • 举报
回复
上面后面一段是多余的.

其实应该这样,12点之前取最小的,12点之后取最大的.

declare @t table(id int, name varchar(10), time datetime)
insert into @t
select 1, '李' , '2006-10-10 09:00:00'
union all select 1, '李' , '2006-10-10 09:00:00'
union all select 2, '李' , '2006-10-10 10:00:00'
union all select 3, '李' , '2006-10-10 18:00:00'
union all select 4, '李' , '2006-10-11 09:00:00'
union all select 5, '李' , '2006-10-11 18:00:00'

select name , convert(varchar(10),time,120) as time , min(time) as '打卡记录' from @t where convert(varchar(10),time,114) < '12:00:00' group by name , convert(varchar(10),time,120)
union all
select name , convert(varchar(10),time,120) as time , max(time) as '打卡记录' from @t where convert(varchar(10),time,114) > '12:00:00' group by name , convert(varchar(10),time,120)
order by name , time

name time 打卡记录
---------- ---------- ------------------------------------------------------
李 2006-10-10 2006-10-10 09:00:00.000
李 2006-10-10 2006-10-10 18:00:00.000
李 2006-10-11 2006-10-11 09:00:00.000
李 2006-10-11 2006-10-11 18:00:00.000

(所影响的行数为 4 行)
micrsoft3 2006-10-30
  • 打赏
  • 举报
回复
没想到大家这么热情~

我主要是想这样,删除表中,当天每人打卡超过三次的,删除前一次和后一次中间的记录.
dawugui 2006-10-30
  • 打赏
  • 举报
回复
其实应该这样,12点之前取最小的,12点之后取最大的.

declare @t table(id int, name varchar(10), time datetime)
insert into @t
select 1, '李' , '2006-10-10 09:00:00'
union all select 1, '李' , '2006-10-10 09:00:00'
union all select 2, '李' , '2006-10-10 10:00:00'
union all select 3, '李' , '2006-10-10 18:00:00'
union all select 4, '李' , '2006-10-11 09:00:00'
union all select 5, '李' , '2006-10-11 18:00:00'

select name , convert(varchar(10),time,120) as time , min(time) as '打卡记录' from @t where convert(varchar(10),time,114) < '12:00:00' group by name , convert(varchar(10),time,120)
union all
select name , convert(varchar(10),time,120) as time , max(time) as '打卡记录' from @t where convert(varchar(10),time,114) > '12:00:00' group by name , convert(varchar(10),time,120)
order by name , time

name time 打卡记录
---------- ---------- ------------------------------------------------------
李 2006-10-10 2006-10-10 09:00:00.000
李 2006-10-10 2006-10-10 18:00:00.000
李 2006-10-11 2006-10-11 09:00:00.000
李 2006-10-11 2006-10-11 18:00:00.000

(所影响的行数为 4 行)


select name , min(time) from @t where convert(varchar(10),time,114) < '12:00:00' group by name
union all
select name , max(time) from @t where convert(varchar(10),time,114) > '12:00:00' group by name
order by name
taochunsong 2006-10-30
  • 打赏
  • 举报
回复
不是这样考虑的
考勤的话
至少还有一个排班的表

要根据那个表里面的数据进行计算的,一般不是删除数据的,
xiaoku 2006-10-30
  • 打赏
  • 举报
回复
--我想的复杂了
--try
create table table1
(
id int IDENTITY( 1,1) not null ,
name varchar(10),
时间 datetime
)
insert into table1
select '李','2006-10-10 09:00:00' union all
select '李','2006-10-10 10:00:00' union all
select '李','2006-10-10 18:00:00' union all
select '李','2006-10-11 09:00:00' union all
select '李','2006-10-11 18:00:00'

select *from table1
--删除语句
delete
from table1
where 时间<(
select max(时间)
from table1
where name =table1.name
group by name,convert(varchar(10),时间,120)
having count(name)>=3
)
and 时间>(
select min(时间)
from table1
where name =table1.name
group by name,convert(varchar(10),时间,120)
having count(name)>=3
)

select *from table1
--结果
id name 时间
----------- ---------- ------------------------------------------------------
1 李 2006-10-10 09:00:00.000
3 李 2006-10-10 18:00:00.000
4 李 2006-10-11 09:00:00.000
5 李 2006-10-11 18:00:00.000

(所影响的行数为 4 行)

--
Softlee81307 2006-10-30
  • 打赏
  • 举报
回复
Create Table Table1(id int,name varchar(10),time datetime)
Insert into table1
Select 1, '李', '2006-10-10 09:00:00' union all
Select 2, '李', '2006-10-10 10:00:00' union all
Select 3, '李', '2006-10-10 18:00:00' union all
Select 4, '李', '2006-10-11 09:00:00' union all
Select 5, '李', '2006-10-11 18:00:00'
--------------------删除---------------------------------------
Delete Table1 from table1 A inner join
(Select minid=min(id),maxid=max(id),name from table1 group by name) b on a.name=b.name
where a.id<>b.minid And a.id<>b.maxid
-----------------结果----------------------------------
select * from Table1
------------------------------------------------------
id name time
1 李 2006-10-10 09:00:00.000
5 李 2006-10-11 18:00:00.000
hhhdyj 2006-10-30
  • 打赏
  • 举报
回复
不好意思,刚才写的有问题
declare @t table(id int, name varchar(10), time datetime)
insert into @t
select 1, '李' , '2006-10-10 09:00:00'
union all select 2, '李' , '2006-10-10 10:00:00'
union all select 3, '李' , '2006-10-10 18:00:00'
union all select 4, '李' , '2006-10-11 09:00:00'
union all select 5, '李' , '2006-10-11 18:00:00'

select max(time) from @t group by name, convert(varchar,time,111)
delete @t
from @t a
where time <> (select min(time) from @t where name = a.name and convert(varchar,time,111) = convert(varchar,a.time,111) group by name, convert(varchar,time,111))
and time <> (select max(time) from @t where name = a.name and convert(varchar,time,111) = convert(varchar,a.time,111) group by name, convert(varchar,time,111))

select * from @t
i9988 2006-10-30
  • 打赏
  • 举报
回复
上面只针对一天只要求打上下班两次的卡的

一般考勤是应该有排班表的,根据排班表的上下班时间,前后半小时或者一小时算有效打卡,有效打卡重复的上班取最早的,下班取最晚的
hhhdyj 2006-10-30
  • 打赏
  • 举报
回复
delete Table1
where 时间 <> (select min(时间) from Table1 group by name, convert(varchar,时间,111)) and
时间 <> (select max(时间) from Table1 group by name, convert(varchar,时间,111))
oop80 2006-10-30
  • 打赏
  • 举报
回复
--上面的多了点东西,这个是对的。

declare @tmp table
(
name varchar(100),
mintime datetime,
maxtime datetime
)

select name, min(时间), max(时间) into @tmp from table1
group by year(时间), month(时间), day(时间), name

delete from table1

insert into table1
(name, 时间)
select name, mintime
from @tmp

insert into table1
(name, 时间)
select name, maxtime
from @tmp
i9988 2006-10-30
  • 打赏
  • 举报
回复
如果一天只有2次,倒是可以的

delete a
from table1 a
where exists (
select 1 from table1
where name=a.name
and convert(char(10),时间,120)=convert(char(10),a.时间,120)
and 时间<a.时间
)
and exists (
select 1 from table1
where name=a.name
and convert(char(10),时间,120)=convert(char(10),a.时间,120)
and 时间>a.时间
)
xiaoku 2006-10-30
  • 打赏
  • 举报
回复
--?
考勤记录中,不会存在倒班的情况的话,那就简单了很多!
delete from Table1
where right(convert(varchar(13),时间,120),) between '10' and '15'--剔除了早退和迟到的记录

其实你这个问题,好像也可以想得复杂点
oop80 2006-10-30
  • 打赏
  • 举报
回复
没经过测试,小心点用 :)
oop80 2006-10-30
  • 打赏
  • 举报
回复
select * from
(select sum(yg), year(Time) y, month(Time) m from 表b
group by year(Time), month(Time)) a inner join

(select sum(sales), year(Time) y , month(Time) m from 表a
group by year(Time), month(Time)) b

on a.y=b.y and a.m=b.m

declare @tmp table
(
name varchar(100),
mintime datetime,
maxtime datetime
)

select name, min(时间), max(时间) into @tmp from table1
group by year(时间), month(时间), day(时间), name

delete from table1

insert into table1
(name, 时间)
select name, mintime
from @tmp

insert into table1
(name, 时间)
select name, maxtime
from @tmp
jiewenzhai 2006-10-30
  • 打赏
  • 举报
回复
不清楚,帮顶
代码下载地址: https://pan.quark.cn/s/68a5707ee00b 电力用户用电信息采集系统在现代智能电网中扮演着关键角色,它借助通信协议对电力用户的用电数据进行即时或定期的收集和处理。在该系统中,通信协议构成了数据交互的核心,保障了不同设备间信息传输的精确性和完整性。提及的几种通信协议,如376.1、376.2、376.3、645以及698,均为电力行业普遍采纳的标准。1. 376.1/376.2/376.3协议:这些协议通常被划入DL/T 645系列,是中国电力行业创设的电能表通信规范。DL/T 645是一种通用的电能表通信准则,旨在电能表与集中器、采集器等设备间的数据互通。376.1界定了基础的指令格式和数据构造,376.2则增设了更多的功能码和数据字段,而376.3进一步强化了安全验证和加密措施,以保障数据传输的安全性。2. 645协议(DL/T 645-1997):此为早期版本,明确了电能表与远程终端之间的通讯准则,涵盖数据帧布局、指令码、应答模式等。主要应用于远程抄表和操控,如获取电表数据、设定参数、实施控制操作等。3. 698协议(Q/GDW 698-2013):这是一套针对智能电表的通信规范,主要用于智能电表与电力主站间的数据互换。698协议不仅涉及数据采集、控制指令的传递,还包括事件记录、安全防护、时钟同步等多个维度,满足了智能电网对数据采集的高标准和复杂性。测试工具在电力用户用电信息采集系统中的效用不容小觑,它们能够辅助开发者和运维人员核实通信协议的准确性,确保设备间的数据交换遵循标准。测试工具通常整合了模拟器、分析器及调试器等特性,能够模仿多种通信环境,检测数据传输的精确度和稳定性,同时也有助于识别并纠正潜在的通信缺陷。...
已经博主授权,源码转载自 https://pan.quark.cn/s/cf673bbe4a2d 在信息技术领域中,数据库迁移同步是一项关键的工作,特别是在企业系统进行升级改造、多环境数据保持一致性或分布式系统数据整合的场景下。本文将系统性地阐述如何运用Java编程语言高效地达成数据库迁移同步的目标。首先需要掌握数据库迁移的基本定义。数据库迁移指的是将数据从一种数据库系统转移到另一种数据库系统的行为,或者在同一个系统内不同版本之间进行数据传递。这一过程通常包含数据的提取、转换和加载(ETL)环节,同时必须保证数据的统一性和完整性。Java作为一种应用广泛的编程语言,提供了丰富的库和工具用于数据库管理,例如JDBC(Java Database Connectivity)是Java程序访问数据库的标准接口。借助JDBC,我们能够与不同的数据库建立连接,执行SQL指令,以及进行数据的读取与写入操作。在开展数据库迁移工作时,可以通过JDBC同时连接源数据库与目标数据库,以此完成数据的复制任务。数据库迁移同步的关键操作环节包括:1. **建立连接**:借助JDBC驱动程序建立与源数据库和目标数据库的通信通道。必须确保数据库的URL地址、用户账号以及密码设置正确无误。2. **获取表结构**:通过查询`INFORMATION_SCHEMA`或者数据库特有的系统视图,获取源数据库中各个表的构造信息,例如字段名称、数据类型、主键标识等。3. **数据抽取**:利用SQL查询命令从源数据库中选取需要迁移的数据,这些数据可以是全部记录,也可以是基于特定条件的筛选结果。4. **数据转换**:依据源数据库与目标数据库之间的差异,对数据格式进行必要的调整。这可能涉及到数据类型之间的映射、日期格式...

22,296

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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