Click here to Skip to main content
16,016,527 members
Home / Discussions / C#
   

C#

 
Questioncalculating total time betweem time ranges Pin
devil54e3-Sep-14 0:28
devil54e3-Sep-14 0:28 
AnswerRe: calculating total time betweem time ranges Pin
Richard MacCutchan3-Sep-14 0:55
mveRichard MacCutchan3-Sep-14 0:55 
GeneralRe: calculating total time betweem time ranges Pin
devil54e3-Sep-14 1:12
devil54e3-Sep-14 1:12 
GeneralRe: calculating total time betweem time ranges Pin
Richard MacCutchan3-Sep-14 1:28
mveRichard MacCutchan3-Sep-14 1:28 
GeneralRe: calculating total time betweem time ranges Pin
devil54e3-Sep-14 2:13
devil54e3-Sep-14 2:13 
AnswerRe: calculating total time betweem time ranges Pin
Pete O'Hanlon3-Sep-14 1:09
mvePete O'Hanlon3-Sep-14 1:09 
GeneralRe: calculating total time betweem time ranges Pin
devil54e3-Sep-14 1:17
devil54e3-Sep-14 1:17 
AnswerRe: calculating total time betweem time ranges Pin
Richard Deeming3-Sep-14 1:41
mveRichard Deeming3-Sep-14 1:41 
To do this in SQL, CROSS APPLY might help:
SQL
DECLARE @Data TABLE
(
    TimeLogged    datetime2(0) NOT NULL,
    Action        varchar(5) NOT NULL
);

INSERT INTO @Data
VALUES
    ('20140903 12:50:15', 'start'),
    ('20140903 13:45:25', 'idle'),
    ('20140903 13:56:06', 'Start'),
    ('20140903 16:30:35', 'idle')
;

SELECT
    D1.Action,
    Sum(DateDiff(second, D1.TimeLogged, D2.TimeLogged)) As DurationInSeconds
FROM
    @Data As D1
    CROSS APPLY
    (
        SELECT TOP 1
            TimeLogged
        FROM
            @Data As D2
        WHERE
            D2.Action != D1.Action
        And
            D2.TimeLogged >= D1.TimeLogged
        ORDER BY
            TimeLogged
    ) As D2
GROUP BY
    D1.Action
;

/*
Results:

Action	DurationInSeconds
idle	641
start	12579
*/




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: calculating total time betweem time ranges Pin
devil54e3-Sep-14 2:12
devil54e3-Sep-14 2:12 
GeneralRe: calculating total time betweem time ranges Pin
Richard Deeming3-Sep-14 2:18
mveRichard Deeming3-Sep-14 2:18 
GeneralRe: calculating total time betweem time ranges Pin
devil54e3-Sep-14 2:32
devil54e3-Sep-14 2:32 
QuestionMessage Closed Pin
2-Sep-14 0:42
Group 110523772-Sep-14 0:42 
AnswerRe: mask attack Pin
Rob Philpott2-Sep-14 0:44
Rob Philpott2-Sep-14 0:44 
GeneralRe: mask attack Pin
Group 110523772-Sep-14 1:03
Group 110523772-Sep-14 1:03 
GeneralRe: mask attack Pin
Chris Quinn2-Sep-14 3:02
Chris Quinn2-Sep-14 3:02 
AnswerRe: mask attack Pin
Manfred Rudolf Bihy2-Sep-14 0:56
professionalManfred Rudolf Bihy2-Sep-14 0:56 
AnswerRe: mask attack Pin
Dave Kreskowiak2-Sep-14 1:54
mveDave Kreskowiak2-Sep-14 1:54 
RantRe: mask attack Pin
Eddy Vluggen2-Sep-14 2:57
professionalEddy Vluggen2-Sep-14 2:57 
GeneralRe: mask attack Pin
Richard Deeming2-Sep-14 3:22
mveRichard Deeming2-Sep-14 3:22 
GeneralRe: mask attack Pin
Eddy Vluggen2-Sep-14 3:25
professionalEddy Vluggen2-Sep-14 3:25 
GeneralRe: mask attack Pin
Richard Deeming2-Sep-14 3:45
mveRichard Deeming2-Sep-14 3:45 
GeneralRe: mask attack Pin
Eddy Vluggen2-Sep-14 7:36
professionalEddy Vluggen2-Sep-14 7:36 
GeneralRe: mask attack Pin
Dave Kreskowiak2-Sep-14 4:23
mveDave Kreskowiak2-Sep-14 4:23 
GeneralRe: mask attack Pin
OriginalGriff2-Sep-14 5:59
mveOriginalGriff2-Sep-14 5:59 
AnswerRe: mask attack Pin
Eddy Vluggen2-Sep-14 3:00
professionalEddy Vluggen2-Sep-14 3:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.