2013-08-14 10 views
7

ich ein Problem bin vor die Summe der Werte bei der Suche in einer Spalte gespeichert,Wie Summe der Zeitfeld in SQL Server 2008 erhalten

ich eine Tabelle wie folgt aus:

gs_cycle_no | from_time | to_time | total_hours(varchar) ... 
GSC-334/2012 | 13:00  | 7:00  | 42:00 
GSC-334/2012 | 8:30  | 3:45  | 6:00 
. 
. 
. 

Was i zu finden ist die Sum(total_hours) Gruppe von gs_cycle_no. Aber die Sum Methode wird auf der Varchar-Spalte nicht funktionieren und auch kann ich nicht konvertiert es aufgrund ihres Formats in Dezimalzahlen,

Wie kann ich die sum von total_hours Spalte finden, basierend auf gs_cycle_no?

+1

Was bedeutet der Wert '42: 00'? 42 Stunden und 0 Minuten? – GolfWolf

Antwort

5

, wenn Sie keine Minute und nur wenige Stunden, dann können Sie so etwas wie:

select 
    cast(sum(cast(replace(total_hours, ':', '') as int)/100) as nvarchar(max)) + ':00' 
from Table1 
group by gs_cycle_no 

, wenn Sie nicht, versuchen Sie dies:

with cte as 
(
    select 
     gs_cycle_no, 
     sum(cast(left(total_hours, len(total_hours) - 3) as int)) as h, 
     sum(cast(right(total_hours, 2) as int)) as m 
    from Table1 
    group by gs_cycle_no 
) 
select 
    gs_cycle_no, 
    cast(h + m/60 as nvarchar(max)) + ':' + 
    right('00' + cast(m % 60 as nvarchar(max)), 2) 
from cte 

sql fiddle demo

+0

erste Arbeit nur für ganze Stunden wie geschrieben, verpasste Gruppe von in Sekunde, danke dafür –

+0

72:25 - siehe http://sqlfiddle.com/#!3/b81ab/1 –

+0

@OzrenTkalcecKrznaric wie hast du das getestet? :) –

0

Diese Abfrage Finds Summe in Minuten:

SQLFiddle demo

select gs_cycle_no, 

    SUM(
    CAST(
    ISNULL(
    substring(total_hours,1,CHARINDEX(':',total_hours)-1) 
    ,'0') as INT) * 60 
    + 
    CAST(
    ISNULL(
    substring(total_hours,CHARINDEX(':',total_hours)+1,100) 
    ,'0') as INT) 
    ) 
    from t 
group by gs_cycle_no 
+0

Resultset gibt Minuten statt Stunden aus – OzrenTkalcecKrznaric

0

Hier Lösung, wo ich varchar in zwei kleine Stücke, Stunden und Minuten Pause, und dann von ihnen machen Minuten, und schließlich, SUM sie:

SELECT 
    gs_cycle_no, 
    CAST(SUM(
     SUBSTRING(total_hours,0 ,CHARINDEX(':', total_hours)) * 60 + 
     SUBSTRING(total_hours, CHARINDEX(':', total_hours) + 1, LEN(total_hours)))/60 AS VARCHAR) + ':' + 
    CAST(SUM(
     SUBSTRING(total_hours,0 ,CHARINDEX(':', total_hours)) * 60 + 
     SUBSTRING(total_hours, CHARINDEX(':', total_hours) + 1, LEN(total_hours))) % 60 AS VARCHAR) 
FROM Table1 
GROUP BY gs_cycle_no 
1

Diese Arbeit wird:

;with times as (
    select gs_cycle_no = 'GSC-334/2012', total_hours = '8:35' 
    union all SELECT gs_cycle_no = 'GSC-334/2012', '5:00' 
    union all SELECT gs_cycle_no = 'GSC-334/2012', '16:50' 
    union all SELECT gs_cycle_no = 'GSC-334/2012', '42:00' 
    union all SELECT gs_cycle_no = 'GSC-335/2012', '0:00' 
    union all SELECT gs_cycle_no = 'GSC-335/2012', '175:52' 
    union all SELECT gs_cycle_no = 'GSC-335/2012', '12:25') 
SELECT 
    gs_cycle_no, 
    hrs = sum(mins)/60 + sum(hrs), 
    mins = sum(mins) % 60 
FROM 
    TIMES 
    cross apply(
     select c = charindex(':', total_hours) 
    ) idx 
    cross apply(
     select 
      hrs = cast(substring(total_hours, 1, c - 1) as int), 
      mins = cast(substring(total_hours, c + 1, len(total_hours)) as int) 
    ) ext 
group by gs_cycle_no 
order by gs_cycle_no