有表a 包含
名称 编码 时间
张 002 2010-10-04
王 005 2010-10-03
赵 004 2010-10-05
张 002 2010-10-04
孙 007 2010-10-01
王 005 2010-10-03
。
查询结果名称 编码 时间
张 002 2010-10-04
王 005 2010-10-03
张 002 2010-10-04
王 005 2010-10-03
一天内出现两次以上
名称 编码 时间
张 002 2010-10-04
王 005 2010-10-03
赵 004 2010-10-05
张 002 2010-10-04
孙 007 2010-10-01
王 005 2010-10-03
。
查询结果名称 编码 时间
张 002 2010-10-04
王 005 2010-10-03
张 002 2010-10-04
王 005 2010-10-03
一天内出现两次以上
select *
from tb t
where (select count(*) from tb where 名称 = t.名称) >= 2
select *
from tb t
where (select count(*) from tb where 名称 = t.名称 and convert(varchar(8),时间,112) = convert(varchar(8),t.时间,112)) >= 2
where 名称 in (select 名称 from a group by 名称 having count(*)>2)
insert into 表a select '张','002','2010-10-04'
insert into 表a select '王','005','2010-10-03'
insert into 表a select '赵','004','2010-10-05'
insert into 表a select '张','002','2010-10-04'
insert into 表a select '孙','007','2010-10-01'
insert into 表a select '王','005','2010-10-03'
go
select distinct * from 表a
go
drop table 表a
/*
名称 编码 时间
---------- ---------- ----------
孙 007 2010-10-01
王 005 2010-10-03
张 002 2010-10-04
赵 004 2010-10-05(4 行受影响)*/
(select 编码,时间 from tb group by 编码,时间 having count(*)>1) b
where a.编码=b.编码 and a.时间=b.时间
create table 表a(名称 nvarchar(10),编码 nvarchar(10),时间 nvarchar(10))
insert into 表a select '张','002','2010-10-04'
insert into 表a select '王','005','2010-10-03'
insert into 表a select '赵','004','2010-10-05'
insert into 表a select '张','002','2010-10-04'
insert into 表a select '孙','007','2010-10-01'
insert into 表a select '王','005','2010-10-03'
go
select * from 表a a where exists(
select 1 from 表a where 名称=a.名称 and 编码=a.编码 and 时间=a.时间 group by 名称,编码,时间 having count(*)>1
)
go
drop table 表a
/*
名称 编码 时间
---------- ---------- ----------
张 002 2010-10-04
王 005 2010-10-03
张 002 2010-10-04
王 005 2010-10-03(4 行受影响)*/
from tb t
where (select count(*) from tb where 名称 = t.名称
and convert(varchar(8),时间,112) = convert(varchar(8),t.时间,112)) >= 2
写的是对的,就是用convert函数,可能你还要自己微调一下
from a tb1
where (select count(*) from a where 名称 = tb1.名称 and 编码=tb1.编码 and 时间=tb1.时间) >=
*
from
tb
where
名称
in
(select 名称 from tb group by 名称 having count(1)>2)