How to make total?

孤者浪人 提交于 2020-01-25 12:50:09

问题


Using SQLSERVER 2000

How to make a total of intime value

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

Intime Datatype is varchar

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

Before I tried in access 2003

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

Above Query is working perfectly in Access 2003

How to make a total of Intime in sql?

Expected Output

Intime

24:01:00

So on...,

Need Query help


回答1:


try this:

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

OUTPUT:

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

(1 row(s) affected)



回答2:


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


来源:https://stackoverflow.com/questions/1509508/how-to-make-total

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!