How to split normal and overtime hours

From: Andrus <kobruleht2(at)hot(dot)ee>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: How to split normal and overtime hours
Date: 2022-02-12 22:04:13
Message-ID: 9c4036e2-45b1-1322-658f-fad1ee1877b3@hot.ee
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi!

Hours table contains working hours for jobs:

    create table hours (
    jobid integer primary key, -- job done, unique for person
    personid char(10) not null, -- person who did job
    hours numeric(5,2) not null -- hours worked for job
    )

Hours more than 120 are overtime hours.

How to split regular and overtime hours into different columns using
running total by job id and partition by person id?

For example, if John did 3 jobs 1, 2,3 with 90, 50 and 40 hours (total
180 hours) for each job correspondingly, result table should be:

    personid    jobid  normal_hours   overtime_hours
    john            1         90               0
    john            2         30              20
    john            3          0              40

sum on normal_hours column should not be greater than 120 per person.

sum of normal_hours and overtime_hours columns must be same as sum of
hours column in hours table for every person.

Note that since hours running total becomes greater than 120 in job 2,
job 2 hours should appear in both hours columns.

Maybe window functions can used.

Andrus.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andreas 'ads' Scherbaum 2022-02-13 00:11:16 Re: Can we go beyond the standard to make Postgres radically better?
Previous Message Peter J. Holzer 2022-02-12 21:57:02 Re: Can we go beyond the standard to make Postgres radically better?