简体   繁体   中英

Timesheet relational database design

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM