It's hard to answer a question with short description, but i'll try to guess...
Solution for MS SQL Server 2005 and higher.
DECLARE @tbl TABLE (Date_Time DATETIME, [590] INT, [591] INT, [592] INT)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:54:00', 1645, 0, 1)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:54:05', 1645, 0, 1)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:54:10', 1645, 0, 1)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:54:15', 1645, 0, 1)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:54:20', 1645, 0, 1)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:54:25', 1645, 0, 1)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:54:30', 1645, 0, 1)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:54:35', 1645, 0, 0)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:54:40', 1645, 0, 0)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:54:45', 1645, 0, 0)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:54:50', 1646, 0, 1)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:54:55', 1646, 0, 1)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:55:00', 1646, 0, 1)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:55:05', 1646, 0, 1)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:55:10', 1646, 0, 1)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:55:15', 1646, 0, 1)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:55:20', 1646, 0, 1)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:55:25', 1646, 0, 0)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:55:30', 1646, 0, 0)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:55:35', 1646, 0, 0)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:55:40', 1647, 0, 1)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:55:45', 1647, 0, 1)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:55:50', 1647, 0, 1)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:55:55', 1647, 0, 1)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:56:00', 1647, 0, 1)
INSERT INTO @tbl (Date_Time, [590], [591], [592])
VALUES ('2013-03-28 16:56:05', 1647, 0, 1)
DECLARE @tmp TABLE(Process INT, StartDate DATETIME, EndDate DATETIME, DurationInSeconds INT)
INSERT INTO @tmp (Process, StartDate, EndDate, DurationInSeconds)
SELECT T.*, DATEDIFF(ss,[StartDate],[EndDate]) AS DurationInSeconds
FROM (
SELECT t1.[590] AS Process, MIN(t1.Date_Time) AS StartDate, t2.EndDate
FROM @tbl AS t1 LEFT JOIN (
SELECT [590] AS Process, MIN(Date_Time) AS EndDate
FROM @tbl
WHERE [592]=0
GROUP BY [590]
) AS t2 ON t1.[590] = t2.Process
WHERE t1.[592]=1
GROUP BY t1.[590], t2.EndDate
) AS T
ORDER BY T.Process
SELECT *
FROM @tmp
;WITH cte_EndOfProcess AS
(
SELECT ROW_NUMBER() OVER (ORDER BY Process) AS RowNo, Process, EndDate As ProcessEndsAt
FROM @tmp
),
cte_StartNextProcess AS
(
SELECT ROW_NUMBER() OVER (ORDER BY Process) -1 AS RowNo, Process, StartDate As NextProcessStartsAt
FROM @tmp
)
SELECT f.RowNo, f.Process, f.ProcessEndsAt, n.Process AS NextProcess, n.NextProcessStartsAt, DATEDIFF(ss, f.ProcessEndsAt, n.NextProcessStartsAt) AS TimeDiffBetweenProc
FROM cte_EndOfProcess AS f LEFT JOIN cte_StartNextProcess AS n ON f.RowNo = n.RowNo
result of first SELECT statement:
Process StartDate EndDate TimeDiff...
1645 2013-03-28 16:54:00.000 2013-03-28 16:54:35.000 35
1646 2013-03-28 16:54:50.000 2013-03-28 16:55:25.000 35
1647 2013-03-28 16:55:40.000 NULL NULL
result of second SELECT statement (CTE's)
RowNo Process ProcessEndsAt NextP.. NextProcessStartsAt TimeDiff...
1 1645 2013-03-28 16:54:35.000 1646 2013-03-28 16:54:50.000 15
2 1646 2013-03-28 16:55:25.000 1647 2013-03-28 16:55:40.000 15
3 1647 NULL NULL NULL NULL
to count total, use:
SELECT COUNT(f.RowNo) CountOfProcesses, SUM(DATEDIFF(ss, f.ProcessEndsAt, n.NextProcessStartsAt)) AS TotalTime
FROM cte_EndOfProcess AS f LEFT JOIN cte_StartNextProcess AS n ON f.RowNo = n.RowNo
WHERE COALESCE(DATEDIFF(ss, f.ProcessEndsAt, n.NextProcessStartsAt),0)>0
instead of
SELECT f.RowNo, f.Process, f.ProcessEndsAt, n.Process AS NextProcess, n.NextProcessStartsAt, DATEDIFF(ss, f.ProcessEndsAt, n.NextProcessStartsAt) AS TimeDiffBetweenProc
FROM cte_EndOfProcess AS f LEFT JOIN cte_StartNextProcess AS n ON f.RowNo = n.RowNo
result:
CountOfProcesses TotalTime
2 30