日期:2014-05-17  浏览次数:20380 次

数据库求解· 怎么查询
表stuinfo
编号 名字 部门
x x x
表studatetime
编号 上班时间 下班时间  
1 2 012-7-12-10:0 2012-12-10:0
x x x
数据就这样 

2个表连接查询,查出7月份迟到3次的部门和次数,超过9:40这个时间段

------解决方案--------------------
SQL code

  select a.deptno 
  from #stuinfo as a
  inner join #studatetime as b on a.sno = b.sno 
  where b.starttime > '2012-7-01 00:00' 
  and b.starttime < '2012-07-31 23:59'
  and (DATEPART(HOUR, b.starttime) > 9 or (DATEPART(HOUR, b.starttime) = 9 and  DATEPART(MINUTE, b.starttime) > 40))
  group by a.deptno having COUNT(*) >= 3