当前位置: 代码迷 >> Sql Server >> 求sql语句,刚才回答的不对,该如何处理
  详细解决方案

求sql语句,刚才回答的不对,该如何处理

热度:220   发布时间:2016-04-27 19:27:08.0
求sql语句,刚才回答的不对
现在有一张表,有人员编号,日期,时间 三个字段,数据如下: 

  bh date time 
  1 20070809 08:00:00 
  1 20070809 09:00:00 
  1 20070809 17:00:00 
  1 20070810 07:00:00 
  2 20070809 08:00:00 
  3 20070910 08:00:00  
现在要找出每个人 每天 time最大值与最小值的差值,怎么写语句

------解决方案--------------------
SQL code
select bh,date, datediff(ms,date+' ' +max(time), date + ' ' + min(time))from tb  group by bh,date
------解决方案--------------------
SQL code
create table #tt (bh varchar(10),[date] varchar(20),[time] varchar(20))insert into #tt select '1','20070809','08:00:00'union allselect '1','20070809','09:00:00'union allselect '1','20070809','17:00:00'union allselect '1','20070810','07:00:00'union allselect '2','20070809','08:00:00'union allselect '3','20070810','08:00:00'select bh,[date],datediff(minute,min([time]),max([time]))  '相差(分)' from #tt group by bh,[date] order by [date]
  相关解决方案