27,579
社区成员
发帖
与我相关
我的任务
分享
select id,convert(varchar(30),dateadd("hh",-3,t1),111) rq,count(*) from test a
where convert(varchar(30),dateadd("hh",-3,t1),108) between '11:30:00' and '23:59:59'
and not exists (select 1 from test where id = a.id and convert(varchar(30),t1,111) = convert(varchar(30),dateadd("d",1,a.t1),111) and convert(varchar(30),t1,108) between '03:00:00' and '14:30:00')
group by id,convert(varchar(30),dateadd("hh",-3,t1),111)
having count(*)>=2
order by rq
select getdate() --当前时间
select convert(varchar(30),getdate(),111) --只显示日期
select convert(varchar(30),getdate(),108) --只显示时间
select convert(varchar(30),getdate(),120) --完整的
select dateadd("hh",-3,getdate()) -- 当前时间往前移3小时 同样可以改一下数,往前移10个小时
select dateadd("d",1,getdate()) --当前时间的后一天
select id,convert(varchar(30),dateadd("hh",-3,t1),111) rq,count(*) from test a
where convert(varchar(30),dateadd("hh",-3,t1),108) between '11:30:00' and '23:59:59'
and not exists (select 1 from test where convert(varchar(30),t1,111) = convert(varchar(30),dateadd("d",1,a.t1),111) and convert(varchar(30),t1,108) between '03:00:00' and '14:30:00')
group by id,convert(varchar(30),dateadd("hh",-3,t1),111)
having count(*)>=2
order by rq
select id,convert(varchar(30),dateadd("hh",-3,t1),111) rq,count(*) from test
where convert(varchar(30),dateadd("hh",-3,t1),108) between '11:30:00' and '23:59:59'
group by id,convert(varchar(30),dateadd("hh",-3,t1),111)
having count(*)>=2
order by rq
select id,convert(varchar(30),t1,111) rq,count(*) from test
where convert(varchar(30),t1,108) between '06:00:00' and '18:30:00'
group by id,convert(varchar(30),t1,111)
having count(*)>=2
order by rq
create table #t(da datetime,id varchar(10) )
insert into #t
select '2010-09-02 8:10:42',2062 union all
select '2010-09-04 7:21:18',2062union all
select '2010-09-04 16:41:25',2062union all
select '2010-09-05 7:08:13',2062union all
select '2010-09-05 16:40:17',2062union all
select '2010-09-06 15:31:07',2062union all
select '2010-09-07 1:02:29',2062union all
select '2010-09-07 1:02:31',2062union all
select '2010-09-07 15:30:29',2062union all
select '2010-09-08 1:03:18',2062union all
select '2010-09-09 0:14:44',2062union all
select '2010-09-09 8:13:34',2062union all
select '2010-09-09 8:13:36',2062
select id,convert(varchar(10),ta ,126)date ,count(*)co from #t join
(select dateadd(d,number,'2010-09-01') ta from master..spt_values where type='P' AND number<30)as t
on convert(varchar(10),ta,126)= convert(varchar(10),da,126) where
da between convert(varchar(10),da,126)+' 06:00:00.000' and convert(varchar(10),da,126)+' 18:30:00.000'
group by ta,id having count(*)>=2
id date co
---------- ---------- -----------
2062 2010-09-04 2
2062 2010-09-05 2
2062 2010-09-09 2
(3 行受影响)
create table #t(da datetime,id varchar(10) )
insert into #t
select '2010-09-02 8:10:42'union all
select '2010-09-04 7:21:18'union all
select '2010-09-04 16:41:25'union all
select '2010-09-05 7:08:13'union all
select '2010-09-05 16:40:17'union all
select '2010-09-06 15:31:07'union all
select '2010-09-07 1:02:29'union all
select '2010-09-07 1:02:31'union all
select '2010-09-07 15:30:29'union all
select '2010-09-08 1:03:18'union all
select '2010-09-09 0:14:44'union all
select '2010-09-09 8:13:34'union all
select '2010-09-09 8:13:36'
select convert(varchar(10),ta ,126),count(*) from #t join
(select dateadd(d,number,'2010-09-01') ta from master..spt_values where type='P' AND number<30)as t
on convert(varchar(10),ta,126)= convert(varchar(10),da,126) where
da between convert(varchar(10),da,126)+' 06:00:00.000' and convert(varchar(10),da,126)+' 18:30:00.000'
group by ta having count(*)>=2
2010-09-04 2
2010-09-05 2
2010-09-09 2