在子查询中选择2小时之间的最短时间

时间:2018-07-01 08:26:28

标签: sql sql-server

这是我的完整查询:

update dbo.Sheet
set arrival_date =convert(varchar(10),date , 101)
update dbo.Sheet 
       set arival_day =convert(varchar(10),time , 108) 
update dbo.Sheet 
       set leaving_date =convert(varchar(10),date , 101)
update dbo.Sheet
       set leaving_day=convert(varchar(10),time , 108)


	    

       
 

					  
select tFullName
       ,arrival_date
       ,(select  min(arival_day) from dbo.Sheet group by arrival_date having datepart(hh,min(arival_day))  not between 00 and 05)
      ,case when datepart(hh,min(arival_day))  between 00 and 05 then  (select  min(arival_day) from dbo.Sheet where datepart(hh,arival_day)  not between 00 and 05 ) 
	        else min(arival_day)   
	   end arrival_time
	   
	  , case when  lead(datepart(hh,min(arival_day)),1,0)over (order by tfullname) between 00 and 04
	  then lead(leaving_date,1,leaving_date) over (order by tfullname) 
	  else leaving_date end leaving_date
	    
	  ,case when lead(datepart(hh,min(arival_day)),1,0)over (order by tfullname) between 00 and 04
	  then lead((select max(arival_day) from dbo.Sheet where datepart(hh,arival_day) between 00 and 05 group by leaving_date) ,1,max(leaving_day)) over (order by tfullname) 
	  else max(leaving_day)
	  end as leaving_time
	  
	  
FROM dbo.Sheet
group by arrival_date ,leaving_date ,tFullName 

我写这篇文章是为了获得按日期分组的两个小时之间的最小小时数

case 
   when datepart(hh, min(arival_day)) between 00 and 05 
      then (select min(arival_day) 
            from dbo.Sheet 
            where datepart(hh, arival_day) not between 00 and 05 
            group by arrival_date) 
      else 
          min(arival_day)   
end arrival_time

我得到这个错误:

  

子查询返回了多个值。当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。

如何更改它而不在选择附近写上前1个(因为这无济于事)以获得2小时之间的最小小时数?

谢谢

0 个答案:

没有答案