如何从SQL中的别名列获取sum()

时间:2011-06-10 06:59:51

标签: sql sql-server-2005 tsql sum

我想要TotalVisitedTime时间的总和,但它是别名列,那么我怎么能得到它呢?

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

注意:[开始]和[结束]是列名称。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我认为总结秒数然后进行你想要做的转换是有意义的。

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