I want to store such fields in the table as: the ID of the employee, his last name and first name, and how many hours he worked on each day of the month. For example, March 1 – 6 hours, March 2 – 4 hours, March 3 – 4 hours. The example I gave in the table below is how I imagine it, is there a way to store this data in a more convenient way to do it in PostgreSQL? And how to handle the situation when there are 30, 31, 28, or 29 days in the month?
ID | Person`s Full name | 1.03.2023 | 2.03.2023 | 3.03.23 |
---|---|---|---|---|
1 | Bill Gates | 6 | 7 | 6 |
2 | Steve Jobs | 5 | 7 | 6 |
Total worked hours |
---|
19 |
19 |
2
Answers
You’d use two tables: one for employees, and another to store work hours (and that table references its master):
Doing so, you’d enter as many rows into
WORK
table as necessary. For example, for dates when someone doesn’t work (weekends, holidays, vacations), you wouldn’t enter anything at all.Certainly, apply any other constraints you want (valid number of hours, split full name into first and last name, etc.), but that’s the general idea of how to do it.
All you need is:
You don’t need to handle it: the
date
type handles this for you.