我数据库中有一张记录时间的表TableTime
里面有字段:Wid(数字),Ton(datetime:yyyy-MM-dd HH:mm:ss),Toff(datetime:yyyy-MM-dd HH:mm:ss)
分别表示人员id、开始时间、结束时间
现在要查询某个月份内的Wid的总数(重复的算一个),满足Ton或Toff在该月都算
我想用select count(distinct(Wid)) from TableTime where ?
问号之处不知道怎么写,哪位达人能告诉我怎么写,或者给个更好的方法,谢谢。
里面有字段:Wid(数字),Ton(datetime:yyyy-MM-dd HH:mm:ss),Toff(datetime:yyyy-MM-dd HH:mm:ss)
分别表示人员id、开始时间、结束时间
现在要查询某个月份内的Wid的总数(重复的算一个),满足Ton或Toff在该月都算
我想用select count(distinct(Wid)) from TableTime where ?
问号之处不知道怎么写,哪位达人能告诉我怎么写,或者给个更好的方法,谢谢。
select Wid,count(1) from TableTime where datepart(mm,Ton) = 2 or datepart(mm,Toff) = 2 group by Wid
wid,count(distinct(Wid))
from
TableTime
where
datediff(mm,ton,@time)=0 or datediff(mm,toff,@time)=0
select
wid,count(distinct(Wid))
from
TableTime
where
datediff(mm,ton,@time)=0 or datediff(mm,toff,@time)=0
group by
wid
(
wid int,
Ton datetime,
Toff datetime
)
goinsert into TableTime
select 1,'2009-10-1','2009-10-2' union all
select 1,'2009-10-1','2009-10-2' union all
select 1,'2009-11-1','2009-11-2'
godeclare @month int
set @month=11
select wid,count(wid)as num from TableTime where datepart(month,Ton)=@month or datepart(month,Toff)=@month group by wid
where datediff(mm,ton,@time)=0 or datediff(mm,toff,@time)=0