Sv: Sv: Re: Difficulties with LAG-function when calculating overtime

From: Andreas Joseph Krogh <andreas(at)visena(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: Sv: Sv: Re: Difficulties with LAG-function when calculating overtime
Date: 2018-11-19 16:49:22
Message-ID: VisenaEmail.4e.f4aac1334f04dcd5.1672cdde6b4@tc7-visena
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

På mandag 19. november 2018 kl. 17:20:23, skrev Andreas Joseph Krogh <
andreas(at)visena(dot)com <mailto:andreas(at)visena(dot)com>>:
På mandag 19. november 2018 kl. 17:08:57, skrev David G. Johnston <
david(dot)g(dot)johnston(at)gmail(dot)com <mailto:david(dot)g(dot)johnston(at)gmail(dot)com>>:
On Mon, Nov 19, 2018 at 6:24 AM Andreas Joseph Krogh <andreas(at)visena(dot)com
<mailto:andreas(at)visena(dot)com>> wrote:
Anyone has a clever way to solve this kinds of issues and craft a query which
produces the desired result as in the table above?
 
Thinking in terms of theory - you need to calculate the first row and then
calculate the next row using data from the first row.  Then calculate the third
row using data from the second row (you might need to carry-forward some value
from the first row so that the third row can see them...).  That sounds like
the algorithm for iteration which is implemented in SQL via "WITH RECURSIVE".
 
David J.

 
Yea, I kind of figured RECURSIVE CTE was the way foreward...
If anyone has got this working, give me a tip:-)
 
Got it, thanks for getting me on the right track!
 
For the archives:
WITH RECURSIVE prev AS ( SELECT lh.date , lh.balance -- Basis for extra
overtime: balance - compensatory_time , GREATEST(lh.balance -
lh.compensatory_time ,0) AS basis_for_extra_overtime , (GREATEST(lh.balance -
lh.compensatory_time ,0) * lh.overtime_rate/100) as extra_overtime -- balance +
extra_overtime , lh.balance -- extra_overtime + (GREATEST(lh.balance -
lh.compensatory_time ,0) * lh.overtime_rate/100) AS total_time , lh.payout ,
lh.compensatory_time-- Accumulated balance: (total_time - payout -
compensatory_time + "previous month's" accumulated_balance , lh.balance --
extra_overtime + (GREATEST(lh.balance - lh.compensatory_time , 0) *
lh.overtime_rate/100) - lh.payout - lh.compensatory_time AS
accumulated_balance_after_payoutFROM logged_hours lh WHERE lh.date =
'2018-01-01':: DATE UNION ALL SELECT lh.date , lh.balance -- Basis for extra
overtime: balance - compensatory_time - "previous month's"
accumulated_balance_after_payout if negative , GREATEST(lh.balance -
lh.compensatory_time-- minus "previous month's"
accumulated_balance_after_payout if negative + LEAST(coalesce
(prev.accumulated_balance_after_payout,0), 0) , 0) AS basis_for_extra_overtime
, (GREATEST(lh.balance - lh.compensatory_time -- minus "previous month's"
accumulated_balance_after_payout if negative + LEAST(coalesce
(prev.accumulated_balance_after_payout,0), 0) , 0) * lh.overtime_rate/100) as
extra_overtime-- balance + extra_overtime , lh.balance -- extra_overtime + (
GREATEST(lh.balance - lh.compensatory_time -- minus "previous month's"
accumulated_balance_after_payout if negative + LEAST(coalesce
(prev.accumulated_balance_after_payout,0), 0) , 0) * lh.overtime_rate/100) AS
total_time , lh.payout , lh.compensatory_time-- Accumulated balance:
(total_time - payout - compensatory_time + "previous month's"
accumulated_balance , lh.balance -- extra_overtime + (GREATEST(lh.balance -
lh.compensatory_time-- minus "previous month's"
accumulated_balance_after_payout if negative + LEAST(coalesce
(prev.accumulated_balance_after_payout,0), 0) , 0) * lh.overtime_rate/100) -
lh.payout - lh.compensatory_time +coalesce
(prev.accumulated_balance_after_payout,0) AS accumulated_balance_after_payout
FROMlogged_hours lh JOIN prev ON lh.date = prev.date + '1 MONTH'::INTERVAL )
select* from prev;
 
Produces the correct result:
 
date balance basis_for_extra_overtime extra_overtime total_time payout
compensatory_time accumulated_balance_after_payout 2018-01-01 17.50 17.5 8.75
26.25 26.25 0.00 0 2018-02-01 2.50 0 0 2.5 0.00 5.00 -2.5 2018-03-01 14.00 7.5
3.75 17.75 3.75 4.00 7.5 2018-04-01 -10.00 0 0 -10 0.00 10.00 -12.5
 
-- Andreas Joseph Krogh

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Voillequin, Jean-Marc 2018-11-21 14:09:55 Weird "could not determine which collation to use for string comparison" with LEAST/GREATEST on PG11 procedure
Previous Message Andreas Joseph Krogh 2018-11-19 16:20:23 Sv: Re: Difficulties with LAG-function when calculating overtime