It is not entirely clear to me to which table you want to apply the date filter. But the solution approach that you need is clear.
What you need is a SELECT from the main table (EMPLOYEES), followed by LEFT OUTER JOINs for every following table, where the filter on the TIME_STAMP column is part of the ON clause. If you do not apply the date filter as part of the ON clause, it can eliminate rows from the Outer (preserved) table.
For example:
SELECT E.FIRST_NAME,
E.LAST_NAME,
SUM(S.TOTAL) AS TOTAL_SALES,
SUM(I.INCOMING) AS TOTAL_INCOMING
FROM EMPLOYEES E
LEFT JOIN SALES S
ON S.USERID = E.USERID
AND S.TIME_STAMP BETWEEN @START_DATE AND @END_DATE
LEFT JOIN INCOMING I
ON I.USERID = E.USERID
AND I.TIME_STAMP BETWEEN @START_DATE AND @END_DATE
WHERE E.TIME_STAMP BETWEEN @START_DATE AND @END_DATE
GROUP BY E.FIRST_NAME, E.LAST_NAME
Gert-Jan
No comments:
Post a Comment