开发者

how to get sum() from alise column in SQL

开发者 https://www.devze.com 2023-03-12 03:09 出处:网络
I\'d like the sum of TotalVisitedTime time but it is alias column so how can I gtt it? Select CONVERT(varchar(6), DATEDIFF(second, [Start], [End])/3600)

I'd like the sum of TotalVisitedTime time but it is alias column so how can I gtt it?

Select CONVERT(varchar(6), DATEDIFF(second, [Start], [End])/3600)
   + ':'
   + RIGHT('0' + CONVERT(varchar(2), (DATEDIFF(second, [Start], [End])开发者_JS百科 % 3600) / 60), 2)
   + ':'
   + RIGHT('0' + CONVERT(varchar(2), DATEDIFF(second, [Start], [End]) % 60), 2) AS TotalVisitedTime
FROM [Table Name]

Note: [Start] and [End] are columns name.


SELECT SUM(TotalVisitedTime)
FROM (    
    -- AS PER @Alex Aza,  @Andriy M     
        SELECT 1234 AS TotalVisitedTime
     ) AS OuterTable


I think it makes sense to sum up seconds and then do the conversion you are trying to do.

SELECT  CONVERT(varchar(6), diff/3600)
    + ':'
    + RIGHT('0' + CONVERT(varchar(2), (diff % 3600) / 60), 2)
    + ':'
    + RIGHT('0' + CONVERT(varchar(2), diff % 60), 2) AS TotalVisitedTime
FROM 
(
    select sum(DATEDIFF(second, [Start], [End]) diff
    from [Table Name]
) tt
0

精彩评论

暂无评论...
验证码 换一张
取 消