Let me start by saying that I know that my topic is not entirely correct, but I do not know how to express it.
Instead of trying to explain what I'm trying to do, let me show you:
Below is a snippet of the relevant data from my table (I use CONVERT to round the date and time fields to the required values. All dates: "XX / XX / XXXX 00: 00: 00.000" and all the time "01/01/1900 XX: XX: XX. 000 ", where XX is the true value and 00 is the placeholder).
CALLNBR DATE START END
I need to relate the time when one recording end time is the next one start time. I am trying to get a result similar to (Filtered by CALLNBR 182735):
CallNbr DATE t1Start t1end t2Start t2end t3Start t3end ----------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- 0000182735 03/06/2012 14:40:00 15:10:00 15:10:00 15:30:00 15:30:00 16:45:00 0000182735 03/07/2012 09:10:00 10:00:00 10:00:00 10:40:00 NULL NULL
But the result I get:
CallNbr DATE t1Start t1end t2Start t2end t3Start t3end ----------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- 0000182735 03/06/2012 14:40:00 15:10:00 15:10:00 15:30:00 15:30:00 16:45:00 0000182735 03/06/2012 15:10:00 15:30:00 15:30:00 16:45:00 NULL NULL 0000182735 03/07/2012 09:10:00 10:00:00 10:00:00 10:40:00 NULL NULL
My problem is related to the average entry, the values ββof which are indicated in the first entry:
0000182735 03/06/2012 15:10:00 15:30:00 15:30:00 16:45:00 NULL NULL
Unfortunately, I cannot suppress NULLS with t3Start or t3End because it will eliminate the entry for (in this case) 07/03/2012.
0000182735 03/07/2012 09:10:00 10:00:00 10:00:00 10:40:00 NULL NULL
And now for SELECT (to make it more readable, I delete my CONVERT'ers):
SELECT t1.CallNbr, t1.STRTDATE, t1.strttime as t1Start, t1.endtme as t1end, t2.strttime as t2Start, t2.endtme as t2end, t3.strttime as t3Start, t3.endtme as t3end FROM TableA t1 INNER JOIN TableA t2 ON t1.endtme = t2.strttime AND t1.strtdate = t2.strtdate AND t1.CALLNBR = t2.CALLNBR AND t1.LINITMTYP = 'L' LEFT JOIN TableA t3 ON t3.CALLNBR = t2.CALLNBR AND t3.strttime = t2.endtme AND t3.strtdate = t1.strtdate WHERE t1.CALLNBR = '0000182735' ORDER BY t1.CALLNBR, t1.strtdate
Even if you cannot help, thanks for taking the time to read it.
Thanks,
Phillip