Assume a task can have many actions. Now consider these three queries:
Now if I wrote these correctly, all three queries should return the same result set. So my question is:
1. Which query is most efficient (fastest) and
2. Under what condition would it be better to use the other two queries? Adam
Code:
---------Query 1-----------
SELECT
tblTasks.TaskID,
( SELECT Min(ActionDate)
FROM tblActions
WHERE tblActions.TaskID=tblTasks.TaskID
) AS FirstActionDate,
( SELECT Max(ActionDate)
FROM tblActions
WHERE tblActions.TaskID=tblTasks.TaskID
) AS LastActionDate
FROM
tblTasks
--------Query 2-----------
SELECT
tblTasks.TaskID,
FirstAction.dt AS FirstActionDate,
LastAction.dt AS LastActionDate
FROM
tblTasks
LEFT OUTER JOIN
( SELECT Min(ActionDate) As dt,
tblActions.TaskID
FROM tblActions
GROUP BY tblActions.TaskID
) AS FirstAction
ON tblTasks.TaskID=FirstAction.TaskID
LEFT OUTER JOIN
( SELECT Max(ActionDate) As dt,
tblActions.TaskID
FROM tblActions
GROUP BY tblActions.TaskID
) AS LastAction
ON tblTasks.TaskID=LastAction.TaskID
-----------Query 3-------------
SELECT
tblTasks.TaskID,
Min(FirstAction.ActionDate) AS FirstActionDate,
Max(LastAction.ActionDate) AS LastActionDate
FROM
tblTasks
LEFT OUTER JOIN
tblActions FirstAction
ON tblTasks.TaskID=FirstAction.TaskID
LEFT OUTER JOIN
tblActions LastAction
ON tblTasks.TaskID=LastAction.TaskID
GROUP BY tblTasks.TaskID
Now if I wrote these correctly, all three queries should return the same result set. So my question is:
1. Which query is most efficient (fastest) and
2. Under what condition would it be better to use the other two queries? Adam