I have the following data in a table:
Date ID Start END Duration_S Duration_A
9/12/2011 22216 9/12/2011 12:30:00 PM 9/12/2011 2:15:00 PM 6300 NULL
9/12/2011 22216 9/12/2011 2:30:00 PM 9/12/2011 2:39:00 PM 540 NULL
9/12/2011 22216 9/12/2011 5:20:00 PM 9/12/2011 5:50:00 PM 1800 NULL
My goal is to update the Duration_A column, currently Null.
The information I use to get that info is a table structured like so:
ID Start_Time State End_Time State_Duration
22216 6/3/10 2:07:58 1 6/3/10 2:20:58 800
22216 6/3/10 2:21:58 2 6/3/10 2:25:55 52
22216 6/3/10 5:21:58 2 6/3/10 5:31:05 600
To update my Duration_A, I have to sum the duration containing certain states(this is no problem), but the issue is that the Start_Time and End_Time have to match the Start and End of my first table. Here is what I am doing:
SELECT tblB.ID, SUM(tblB.StateDuration) AS Total, tblA.Start_Time,
tblA.End_Time
FROM
tblA INNER JOIN tblB ON tblB.Id = tblA.ID
tblB.Start_Time >= tblA.Start AND
tblB.End_Time <= tblA.End
WHERE STATE IN (1, 2, 3, 4, 5, 6, 7, 10, 11)
GROUP BY tblB.StateDuration, tblB.Id, tblA.Start_Time,
tblA.End_Time,tblB.End_Time,
tblB.Start_Time
This gives me the first duration when the start and end time of tblB match tblA, where I want the sum of all durations between those time frames. If I am not clear, let me know.
As far as I can understand you don’t need to group by all there fields. Just add more conditions into the WHERE clause: