日期:2014-05-18  浏览次数:20426 次

求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 all
select '1','20070809','09:00:00'
union all
select '1','20070809','17:00:00'
union all
select '1','20070810','07:00:00'
union all
select '2','20070809','08:00:00'
union all
select '3','20070810','08:00:00'


select bh,[date],datediff(minute,min([time]),max([time]))  '相差(分)' from #tt group by bh,[date] order by [date]