Question

I'm generating a date series via PostgreSQL's generate_series(min, max) in the following way:

SELECT 
    generate_series(getstartdate(some arguments)
                  , getenddate(some arguments), interval '1 day')
FROM taskresults

getstartdate() and getenddate() are each returning the start- and end date of a given task. I have more tables Employees(employeeid, taskid, worktime) and Tasks(taskid, startdate, enddate).

My goal is to get the employees working time grouped by each day from my generated series. How can I perform this join? Note that I do not have direct access to the columns startdate and enddate in the table Tasks I can only access the dates via those functions mentioned above. The worktime is in hours/day so I have to aggregate it via SUM() for each task the employee works in to the given date in the series. The problem is that I don't how to access a date in the generated series.

EDIT
Data structures:

CREATE TABLE employees
(
  employeeid serial NOT NULL,
  firstname character varying(32),
  lastname character varying(32),
  qualification character varying(32),
  incomeperhour numeric,
)
CREATE TABLE employeetasks
(
  projectid integer,
  taskid integer,
  employeeid integer,
  hoursperday real,
)
CREATE TABLE taskresults
(
  simulationid integer,
  taskid integer,
  duration integer
)
CREATE TABLE tasks
(
  projectid integer NOT NULL,
  taskname character varying(32),
  startdate character varying(32),
  enddate character varying(32),
  predecessor integer,
  minduration integer,
  maxduration integer,
  taskid integer,
)

Some explanation:
The whole database is for simulation so at first you define a task schedule (in table tasks) and then run the simulation that inserts the results into taskresults. As you can see I only store the duration in the results that's why I can only access the date ranges for each task with the getstartdate / getenddate functions. The table employeetasks basically assigns employees from the employees table to the task table with an hour-amount they're working in that task per day.

Was it helpful?

Solution

You can JOIN on the generated series like anything else.

INNER JOIN generate_series(getstartdate(some arguments), getenddate(some arguments), interval '1 day') workday ON (...)

The join condition is hard to work out without knowing how your data is stored.

Also, your data structure is weird. Employees have a "taskid"? n:1 employees -> task? I can't really write a full query 'cos I don't get the data structure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top