10 个解决方案
#1
delete a from 表 a
where exists(
select 1 from 表 where id=a.id and
time <>(select min(time) from 表 where id=a.id )
and
time <>(select max(time) from 表 where id=a.id )
)
#2
select
card_id ,
convert(varchar(10),sign_time,120) as 日期
min(sign_time) as 当天最早,
max(sign_time) as 当天最晚
from
timerecords
group by card_id,convert(varchar(10),sign_time,120)
#3
select * from timerecords a
where not exists(select 1 from timerecords b where a.id=b.id and a.time>b.time )
union all
select * from timerecords a
where not exists(select 1 from timerecords b where a.id=b.id and a.time<b.time )
#4
select * from timerecords a
where not exists(select 1 from timerecords where card_id=a.card_id and datediff
(dd,sign_time,a.sign_time)=0 and sign_time>a.sign_time)
union all
select * from timerecords a
where not exists(select 1 from timerecords where card_id=a.card_id and datediff
(dd,sign_time,a.sign_time)=0 and sign_time<a.sign_time)
#5
--显示为两条记录
select
*
from
timerecords a
where
sign_time=(select max(sign_time) from timerecords where card_id=a.card_id and datediff(d,sign_time,a.sign_time )=0)
or
sign_time=(select min(sign_time) from timerecords where card_id=a.card_id and datediff(d,sign_time,a.sign_time )=0)
#6
哦 我缺了同一天了..
#7
就是在这一个表中执行,需要判断是否是同一个员工的今天的纪录,因为表中纪录的员工数量比较多,时间段也比较长,要考虑当天,不能和以前的时间比较,希望有高手帮忙完善.谢谢
#8
min(time),max(time)
是这意思么……
看你的意思是所有的记录都在一张表内
我想可以做个while循环~循环找每天的min(time),max(time)
是这意思么……
看你的意思是所有的记录都在一张表内
我想可以做个while循环~循环找每天的min(time),max(time)
#9
数据库表为timerecords,里面纪录了每天的员工刷卡的卡号:card_id和时间:sign_time,每个卡号都有多条纪录,现在需要根据每天的刷卡时间来纪录考勤情况,也就是只留下每天每个员工刷卡最早的一次和最晚的一次刷卡纪录,中间其他的纪录删除掉.请高手指教!!
select card_id , min(sign_time) sign_time_min from timerecords group by card_id
union all
select card_id , max(sign_time) sign_time_max from timerecords group by card_id
#10
--如果只有card_id , sign_time两个字段.
select card_id , min(sign_time) sign_time_min from timerecords group by card_id
union all
select card_id , max(sign_time) sign_time_max from timerecords group by card_id
--如果不止card_id , sign_time两个字段.
select t.* from timerecords t where sign_time = (select min(sign_time) from timerecords where card_id = t.card_id)
union all
select t.* from timerecords t where sign_time = (select max(sign_time) from timerecords where card_id = t.card_id)
#1
delete a from 表 a
where exists(
select 1 from 表 where id=a.id and
time <>(select min(time) from 表 where id=a.id )
and
time <>(select max(time) from 表 where id=a.id )
)
#2
select
card_id ,
convert(varchar(10),sign_time,120) as 日期
min(sign_time) as 当天最早,
max(sign_time) as 当天最晚
from
timerecords
group by card_id,convert(varchar(10),sign_time,120)
#3
select * from timerecords a
where not exists(select 1 from timerecords b where a.id=b.id and a.time>b.time )
union all
select * from timerecords a
where not exists(select 1 from timerecords b where a.id=b.id and a.time<b.time )
#4
select * from timerecords a
where not exists(select 1 from timerecords where card_id=a.card_id and datediff
(dd,sign_time,a.sign_time)=0 and sign_time>a.sign_time)
union all
select * from timerecords a
where not exists(select 1 from timerecords where card_id=a.card_id and datediff
(dd,sign_time,a.sign_time)=0 and sign_time<a.sign_time)
#5
--显示为两条记录
select
*
from
timerecords a
where
sign_time=(select max(sign_time) from timerecords where card_id=a.card_id and datediff(d,sign_time,a.sign_time )=0)
or
sign_time=(select min(sign_time) from timerecords where card_id=a.card_id and datediff(d,sign_time,a.sign_time )=0)
#6
哦 我缺了同一天了..
#7
就是在这一个表中执行,需要判断是否是同一个员工的今天的纪录,因为表中纪录的员工数量比较多,时间段也比较长,要考虑当天,不能和以前的时间比较,希望有高手帮忙完善.谢谢
#8
min(time),max(time)
是这意思么……
看你的意思是所有的记录都在一张表内
我想可以做个while循环~循环找每天的min(time),max(time)
是这意思么……
看你的意思是所有的记录都在一张表内
我想可以做个while循环~循环找每天的min(time),max(time)
#9
数据库表为timerecords,里面纪录了每天的员工刷卡的卡号:card_id和时间:sign_time,每个卡号都有多条纪录,现在需要根据每天的刷卡时间来纪录考勤情况,也就是只留下每天每个员工刷卡最早的一次和最晚的一次刷卡纪录,中间其他的纪录删除掉.请高手指教!!
select card_id , min(sign_time) sign_time_min from timerecords group by card_id
union all
select card_id , max(sign_time) sign_time_max from timerecords group by card_id
#10
--如果只有card_id , sign_time两个字段.
select card_id , min(sign_time) sign_time_min from timerecords group by card_id
union all
select card_id , max(sign_time) sign_time_max from timerecords group by card_id
--如果不止card_id , sign_time两个字段.
select t.* from timerecords t where sign_time = (select min(sign_time) from timerecords where card_id = t.card_id)
union all
select t.* from timerecords t where sign_time = (select max(sign_time) from timerecords where card_id = t.card_id)