Reputation: 3591
I need to build some queries on top of an existing timesheet database application for project specific purposes.
I can drop out data from the timesheet database of
USER_ID | ENTRY_DATE | BILLED_HRS
then map it a Project Organisation chart for that the data I have is:
USER_ID | PROJECT_ROLE | START_DATE | FINISH_DATE
what the best way to join these two given that START_DATE and FINISH_DATE are the only way to map HRS to PROJECT_ROLE.
Probably only 5-10 USER_IDs would have multiple roles so there maybe a way to fast track this. I can't modify the underlying timesheet application tables so this join needs to be fast enough to run regularly.
thanks
Upvotes: 0
Views: 301
Reputation: 3043
The basic query you are looking for is something like:
SELECT ts.*, po.project_role
FROM timesheet ts
LEFT JOIN project_organization po ON
po.user_id = ts.user_id AND
ts.entry_date BETWEEN po.start_date AND po.finish_date
I would leave a LEFT JOIN so as to be sure you do not loose timesheet data even if you do not match any project_role. Be careful though... if the project organization table has rows that overlap start_date and finish_date for the same user_id, you will be getting duplicate timesheet data. And that is not avoidable with an INNER vs OUTER JOIN.
As to the efficiency of this query, it very well depends on the indexes you have.
As a personal suggestion... I'd advise to pull out the data regularly (each night maybe) and make a copy in another db for reporting purposes. This way you can check it (showing warnings for data that match no project_role or that match more than one project_role) and cleanse it (writing the project_role found directly on the timesheet data table). Of course you cannot do this if you want to run your queries on live data. But I also suppose timesheet data is input no more than once a day, so it could be the best solution.
Upvotes: 2