怎么做总计?

时间:2009-10-02 13:29:50

标签: sql sql-server tsql sql-server-2000

使用SQLSERVER 2000

如何制作总计的intime值

Table 1
    InTime
    02:00:48
    22:00:22
    .....,

Intime数据类型是varchar

02:00:12 - (HH:MM:SS)

在我尝试访问2003之前

select format( Int(24*sum(Intime)), '0') & format( sum(Intime) , ':ss' ) AS totaltime from table1

以上查询在Access 2003中完美运行

如何在sql中总共使用Intime?

预期产出

Intime

24:01:00

所以......,

需要查询帮助

2 个答案:

答案 0 :(得分:1)

试试这个:

CREATE TABLE #Table1
(
inTime varchar(8)
)

SET NOCOUNT ON
INSERT INTO #Table1 VALUES('02:00:48')
INSERT INTO #Table1 VALUES('22:00:22')
SET NOCOUNT OFF


SELECT
    CONVERT(varchar(2),TotalSeconds/3600)
        +':'
        +RIGHT('00'+CONVERT(varchar(2),(TotalSeconds-(TotalSeconds/3600*3600))/60),2)
        +':'
        +RIGHT('00'+CONVERT(varchar(2),TotalSeconds-((TotalSeconds/3600*3600)+(((TotalSeconds-(TotalSeconds/3600*3600))/60)*60))),2) AS Answer
        ,DATEADD(second,dt.TotalSeconds,CONVERT(datetime,'1/1/1900')) AS AnswerIncrementingDays

    FROM (SELECT
              SUM(DATEDIFF(second,CONVERT(datetime,'1/1/1900'),CONVERT(datetime,'1/1/1900 '+inTime))) AS TotalSeconds
              FROM #Table1
         ) dt

输出:

Answer   AnswerIncrementingDays
-------- -----------------------
24:01:10 1900-01-02 00:01:10.000

(1 row(s) affected)

答案 1 :(得分:0)

select right('0' + cast(TotalHours + TotalMinutes / 60 as varchar), 2) + ':' + right('0' + cast(TotalMinutes % 60 as varchar), 2) + ':' + right('0' + cast(Seconds as varchar), 2)
from (
    select TotalHours, TotalMinutes + TotalSeconds / 60 as TotalMinutes, TotalSeconds % 60 as Seconds
    from (
        select 
            TotalHours = SUM(cast(substring(a, 1, 2) as int)),
            TotalMinutes = SUM(cast(substring(a, 4, 2) as int)),
            TotalSeconds = SUM(cast(substring(a, 7, 2) as int))
        from (
            select '02:00:48' a
            union
            select '22:00:22'
        ) a
    ) b
) c