Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nJeKP-00039b-8H for pgsql-general@arkaria.postgresql.org; Mon, 14 Feb 2022 16:35:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nJeKM-00086c-SU for pgsql-general@arkaria.postgresql.org; Mon, 14 Feb 2022 16:35:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nJERg-0006Bf-Ej for pgsql-general@lists.postgresql.org; Sun, 13 Feb 2022 12:57:40 +0000 Received: from portal-smtp-out.hot.ee ([194.126.101.125]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nJERc-0002Vq-F2 for pgsql-general@postgresql.org; Sun, 13 Feb 2022 12:57:39 +0000 Received: from localhost (localhost [127.0.0.1]) by vm-hot-relay10.estpak.ee (Postfix) with ESMTP id 6590916C; Sun, 13 Feb 2022 14:57:33 +0200 (EET) X-Virus-Scanned: Debian amavisd-new at vm-hot-relay10.estpak.ee Received: from portal-smtp-out.hot.ee ([127.0.0.1]) by localhost (vm-hot-relay10.estpak.ee [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id L1KnyB42yooX; Sun, 13 Feb 2022 14:57:30 +0200 (EET) Received: from mail.hot.ee (hot-relayhost1.estpak.ee [213.180.2.79]) by vm-hot-relay10.estpak.ee (Postfix) with ESMTP id A954D2639; Sun, 13 Feb 2022 14:57:30 +0200 (EET) Content-Type: multipart/alternative; boundary="------------ubwGdAwiqvDCRtO80BKtiGPM" Message-ID: <4a5aa9e4-c47c-5d26-596b-8b0511061977@hot.ee> Date: Sun, 13 Feb 2022 14:57:25 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.6.0 Subject: Re: How to split normal and overtime hours Content-Language: et To: =?UTF-8?Q?Torsten_F=c3=b6rtsch?= Cc: pgsql-general References: <9c4036e2-45b1-1322-658f-fad1ee1877b3@hot.ee> From: Andrus In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------ubwGdAwiqvDCRtO80BKtiGPM Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi! Thank you. In this result, regular and overtime columns contain running totals. How to fix this so that those columns contain just hours for each job? sum on regular column should not be greater than 120 per person. sum of regular and overtime  columns must be same as sum of hours column in hours table for every person. Andrus. 13.02.2022 14:46 Torsten Förtsch kirjutas: > something like > > SELECT * >      , least(sum(hours) OVER w, 120) AS regular >      , greatest(sum(hours) OVER w - 120, 0) AS overtime >   FROM hours > WINDOW w AS (PARTITION BY person ORDER BY job_id); > >  job_id | person | hours | regular | overtime > --------+--------+-------+---------+---------- >       2 | bill   |    10 |      10 |        0 >       5 | bill   |    40 |      50 |        0 >       8 | bill   |    10 |      60 |        0 >      10 | bill   |    70 |     120 |       10 >      11 | bill   |    30 |     120 |       40 >      13 | bill   |    40 |     120 |       80 >      15 | bill   |    10 |     120 |       90 >       4 | hugo   |    70 |      70 |        0 >       7 | hugo   |   130 |     120 |       80 >       1 | john   |    10 |      10 |        0 >       3 | john   |    50 |      60 |        0 >       6 | john   |    30 |      90 |        0 >       9 | john   |    50 |     120 |       20 >      12 | john   |    30 |     120 |       50 >      14 | john   |    50 |     120 |      100 > > > On Sun, Feb 13, 2022 at 12:47 PM Andrus wrote: > > 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. > --------------ubwGdAwiqvDCRtO80BKtiGPM Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

Hi!

Thank you. In this result, regular and overtime columns contain running totals.

How to fix this so that those columns contain just hours for each job?

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

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

Andrus.

13.02.2022 14:46 Torsten Förtsch kirjutas:
something like

SELECT *
     , least(sum(hours) OVER w, 120) AS regular
     , greatest(sum(hours) OVER w - 120, 0) AS overtime
  FROM hours
WINDOW w AS (PARTITION BY person ORDER BY job_id);

 job_id | person | hours | regular | overtime
--------+--------+-------+---------+----------
      2 | bill   |    10 |      10 |        0
      5 | bill   |    40 |      50 |        0
      8 | bill   |    10 |      60 |        0
     10 | bill   |    70 |     120 |       10
     11 | bill   |    30 |     120 |       40
     13 | bill   |    40 |     120 |       80
     15 | bill   |    10 |     120 |       90
      4 | hugo   |    70 |      70 |        0
      7 | hugo   |   130 |     120 |       80
      1 | john   |    10 |      10 |        0
      3 | john   |    50 |      60 |        0
      6 | john   |    30 |      90 |        0
      9 | john   |    50 |     120 |       20
     12 | john   |    30 |     120 |       50
     14 | john   |    50 |     120 |      100


On Sun, Feb 13, 2022 at 12:47 PM Andrus <kobruleht2@hot.ee> wrote:

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.

--------------ubwGdAwiqvDCRtO80BKtiGPM--