寻一个sql语句

特别安静 2009-12-09 02:49:59
我数据库中有一张记录时间的表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 ?
问号之处不知道怎么写,哪位达人能告诉我怎么写,或者给个更好的方法,谢谢。
...全文
72 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rotel-刘志东 2009-12-09
  • 打赏
  • 举报
回复
select wid,count(distinct(Wid)) from TableTime
where datediff(mm,ton,@time)=0 or datediff(mm,toff,@time)=0
特别安静 2009-12-09
  • 打赏
  • 举报
回复
可不可以用年和月一起做条件?
gaopeng1988 2009-12-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 fredrickhu 的回复:]
SQL code--修改下select
wid,count(distinct(Wid))from
TableTimewheredatediff(mm,ton,@time)=0ordatediff(mm,toff,@time)=0groupby
wid
[/Quote]

同意
qiqi860819 2009-12-09
  • 打赏
  • 举报
回复
不知道是不是这样,希望有所帮助

create table TableTime
(
wid int,
Ton datetime,
Toff datetime
)
go

insert 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'
go

declare @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

--小F-- 2009-12-09
  • 打赏
  • 举报
回复
--修改下
select
wid,count(distinct(Wid))
from
TableTime
where
datediff(mm,ton,@time)=0 or datediff(mm,toff,@time)=0
group by
wid
--小F-- 2009-12-09
  • 打赏
  • 举报
回复
select 
wid,count(distinct(Wid))
from
TableTime
where
datediff(mm,ton,@time)=0 or datediff(mm,toff,@time)=0
fwacky 2009-12-09
  • 打赏
  • 举报
回复
上面我写的是 2月份的,你可以改改!
fwacky 2009-12-09
  • 打赏
  • 举报
回复

select Wid,count(1) from TableTime where datepart(mm,Ton) = 2 or datepart(mm,Toff) = 2 group by Wid
特别安静 2009-12-09
  • 打赏
  • 举报
回复
来个高手帮帮我哦
aimee_99 2009-12-09
  • 打赏
  • 举报
回复
强大

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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