views:

50

answers:

2

Simplified table structure (the tables can't be merged at this time):

TableA:

dts_received (datetime)
dts_completed (datetime)
task_a (varchar)

TableB:

dts_started (datetime)
task_b (varchar)

What I would like to do is determine how long a task took to complete.

The join parameter would be something like

ON task_a = task_b AND dts_completed < dts_started

The issue is that there may be multiple date-times that occur after the dts_completed.

How do I create a join that only returns the first tableB-datetime that occurs after the tableA-datetime?

+3  A: 
select a.task_a as task, min(b.dts_started) as dts_started
from TableA a
inner join TableB b on a.task_a = b.task_b 
    and a.dts_completed < b.dts_started 
GROUP BY a.task_a
RedFilter
`GROUP BY a.task_a`
a1ex07
Thanks, cut and paste error!
RedFilter
+2  A: 
SELECT task_a, dts_completed,
bb.started
FROM tableA
INNER JOIN 
(SELECT task_b, MIN(dts_started) as started
FROM tableB GROUP BY taks_b)bb
ON (bb.task_b = tableA.task_a AND bb.started > tableA.dts_completed)
a1ex07