如果时间超过60分钟,则在sql server中以小时显示

时间:2014-03-30 12:51:45

标签: sql sql-server stored-procedures

我有一个存储过程,如:

ALTER procedure [dbo].[IBS_Podiumsummryaveragetime]
@locid integer=null
as 
begin
set nocount on;
select  avg( datediff(mi,t.dtime, t.DelDate )) as Avgstaytime,
 avg( datediff(mi,t.dtime, t.PAICdate )) as  Avgparkingtime, 
 avg( datediff(mi,t.Paydate, t.DelDate )) as AvgDelivarytime 
 from (select top 10 * from transaction_tbl where locid=@locid and dtime >= getdate()-7 order by transactID desc ) t  
 end

如果我的平均时间超过60分钟,那么我想在几个小时内显示......我可以这样做吗? 任何帮助都很明显?

2 个答案:

答案 0 :(得分:1)

我无法在评论中输入此内容,因此我已将其添加为答案:

SELECT 
   CASE 
      WHEN avg( datediff(mi,t.dtime, t.DelDate )) <= 60 
          THEN CAST(avg( datediff(mi,t.dtime, t.DelDate )), varchar)
      ELSE 
          CAST(avg( datediff(mi,t.dtime, t.DelDate )) / 60, varchar) + ":" + CAST(avg( datediff(mi,t.dtime, t.DelDate )) % 60, varchar)
   END 

答案 1 :(得分:1)

@ I.K.主题的变化(我投票赞成)

SELECT CASE WHEN avg(datediff(mi,t.dtime, t.DelDate)) > 60 
            THEN CAST(avg(datediff(mi,t.dtime, t.DelDate))/60 AS VARCHAR(3)) + ':' 
            ELSE ''
       END
       + CAST(avg(datediff(mi,t.dtime, t.DelDate)) % 60  AS VARCHAR(2)) 

--OR

SELECT CASE WHEN avg(datediff(hour,t.dtime, t.DelDate)) > 0 
            THEN CAST(avg(datediff(hour,t.dtime, t.DelDate)) AS VARCHAR(3)) + ':' 
            ELSE ''
       END
       + CAST(avg(datediff(mi,t.dtime, t.DelDate)) % 60  AS VARCHAR(2))