pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Need to omit time during weekends from age calculations
7+ messages / 6 participants
[nested] [flat]

* Need to omit time during weekends from age calculations
@ 2021-06-07 19:12  David Gauthier <davegauthierpg@gmail.com>
  0 siblings, 2 replies; 7+ messages in thread

From: David Gauthier @ 2021-06-07 19:12 UTC (permalink / raw)
  To: pgsql-general

Hi:

I suspect I'm not the first to ask about this but couldn't find anything
after googling for a bit.  So here goes....

I'd like to get the "age" difference between two times which span either
all or part of a weekend but exclude any time that transpired during the
weekend.
Example (please pardon the non-timestamp vals here...)

age('Monday-Noon','Prev-Friday-Noon')
would give me '1 day'.

...and...

age('Sunday-Noon','Prev-Friday-Noon')
would give me '12 hours'

You get the picture.

Has this wheel already been invented ?
I don't see an easy way to do this off-hand.
All Ears :-)

Thanks in Advance.

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Need to omit time during weekends from age calculations
@ 2021-06-07 19:17  Ron <ronljohnsonjr@gmail.com>
  parent: David Gauthier <davegauthierpg@gmail.com>
  1 sibling, 1 reply; 7+ messages in thread

From: Ron @ 2021-06-07 19:17 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 6/7/21 2:12 PM, David Gauthier wrote:
> Hi:
>
> I suspect I'm not the first to ask about this but couldn't find anything 
> after googling for a bit.  So here goes....
>
> I'd like to get the "age" difference between two times which span either 
> all or part of a weekend but exclude any time that transpired during the 
> weekend.
> Example (please pardon the non-timestamp vals here...)
>
> age('Monday-Noon','Prev-Friday-Noon')
> would give me '1 day'.
>
> ...and...
>
> age('Sunday-Noon','Prev-Friday-Noon')
> would give me '12 hours'
>
> You get the picture.
>
> Has this wheel already been invented ?
> I don't see an easy way to do this off-hand.
> All Ears :-)
>
> Thanks in Advance.

You seem to be asking about counting work days.  Am I misunderstanding?

-- 
Angular momentum makes the world go 'round.

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Need to omit time during weekends from age calculations
@ 2021-06-07 19:34  Joe Conway <mail@joeconway.com>
  parent: David Gauthier <davegauthierpg@gmail.com>
  1 sibling, 1 reply; 7+ messages in thread

From: Joe Conway @ 2021-06-07 19:34 UTC (permalink / raw)
  To: David Gauthier <davegauthierpg@gmail.com>; pgsql-general

On 6/7/21 3:12 PM, David Gauthier wrote:
> Hi:
> 
> I suspect I'm not the first to ask about this but couldn't find anything 
> after googling for a bit.  So here goes....
> 
> I'd like to get the "age" difference between two times which span either 
> all or part of a weekend but exclude any time that transpired during the 
> weekend.
> Example (please pardon the non-timestamp vals here...)
> 
> age('Monday-Noon','Prev-Friday-Noon')
> would give me '1 day'.
> 
> ...and...
> 
> age('Sunday-Noon','Prev-Friday-Noon')
> would give me '12 hours'
> 
> You get the picture.
> 
> Has this wheel already been invented ?
> I don't see an easy way to do this off-hand.
> All Ears :-)

Perhaps not the prettiest of solutions, but what about something like this?

8<----------------------------------------
CREATE OR REPLACE FUNCTION nonweekendhours(startts timestamptz, endts 
timestamptz)
RETURNS interval AS $$
   SELECT
     (SUM(case when extract(dow from g.ts) > 0
                and extract(dow from g.ts) < 6 then
            1
          else
            0 end) || ' hours')::interval
   FROM generate_series(startts, endts - '1 hour'::interval,'1 hour') AS 
g(ts)
$$ LANGUAGE sql;

SELECT nonweekendhours('2021-06-04 12:00:00','2021-06-06 12:00:00');
  nonweekendhours
-----------------
  12:00:00
(1 row)

SELECT nonweekendhours('2021-06-04 12:00:00','2021-06-07 12:00:00');
  nonweekendhours
-----------------
  24:00:00
(1 row)

SELECT nonweekendhours('2021-06-04 12:00:00','2021-06-11 12:00:00');
  nonweekendhours
-----------------
  120:00:00
(1 row)
8<----------------------------------------

HTH,

Joe
-- 
Crunchy Data - http://crunchydata.com
PostgreSQL Support for Secure Enterprises
Consulting, Training, & Open Source Development





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Need to omit time during weekends from age calculations
@ 2021-06-07 19:39  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Joe Conway <mail@joeconway.com>
  0 siblings, 0 replies; 7+ messages in thread

From: Tom Lane @ 2021-06-07 19:39 UTC (permalink / raw)
  To: Joe Conway <mail@joeconway.com>; +Cc: David Gauthier <davegauthierpg@gmail.com>; pgsql-general

Joe Conway <mail@joeconway.com> writes:
> On 6/7/21 3:12 PM, David Gauthier wrote:
>> I'd like to get the "age" difference between two times which span either 
>> all or part of a weekend but exclude any time that transpired during the 
>> weekend.

I'm a bit suspicious of this problem statement.  I doubt there are many
practical applications where you wouldn't also wish to exclude holidays,
for somebody's definition of holidays.  Of course, that makes it a lot
messier since you need a source of data for that.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Need to omit time during weekends from age calculations
@ 2021-06-07 19:45  Pavel Stehule <pavel.stehule@gmail.com>
  parent: Ron <ronljohnsonjr@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Pavel Stehule @ 2021-06-07 19:45 UTC (permalink / raw)
  To: Ron <ronljohnsonjr@gmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

po 7. 6. 2021 v 21:17 odesílatel Ron <ronljohnsonjr@gmail.com> napsal:

> On 6/7/21 2:12 PM, David Gauthier wrote:
>
> Hi:
>
> I suspect I'm not the first to ask about this but couldn't find anything
> after googling for a bit.  So here goes....
>
> I'd like to get the "age" difference between two times which span either
> all or part of a weekend but exclude any time that transpired during the
> weekend.
> Example (please pardon the non-timestamp vals here...)
>
> age('Monday-Noon','Prev-Friday-Noon')
> would give me '1 day'.
>
> ...and...
>
> age('Sunday-Noon','Prev-Friday-Noon')
> would give me '12 hours'
>
> You get the picture.
>
> Has this wheel already been invented ?
> I don't see an easy way to do this off-hand.
> All Ears :-)
>
> Thanks in Advance.
>
>
> You seem to be asking about counting work days.  Am I misunderstanding?
>

orafce has functions for business calendar  with holidays

https://github.com/orafce/orafce

plvdate.add_bizdays(day date, days int) date

Regards

Pavel


> --
> Angular momentum makes the world go 'round.
>

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Need to omit time during weekends from age calculations
@ 2021-06-07 21:01  David Gauthier <davegauthierpg@gmail.com>
  parent: Pavel Stehule <pavel.stehule@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: David Gauthier @ 2021-06-07 21:01 UTC (permalink / raw)
  To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: Ron <ronljohnsonjr@gmail.com>; pgsql-general <pgsql-general@lists.postgresql.org>

>>You seem to be asking about counting work days.
Ya, counting work days, or discounting non-work days.  Two sides, same coin.

Thanks Joe.  I think the nonweekendhours solution should be good enough for
what I need.

Yes, holidays too would be the best.  But for practical purposes, excluding
Sat&Sun is good enough for this particular problem.

Thanks Everyone !


On Mon, Jun 7, 2021 at 3:46 PM Pavel Stehule <pavel.stehule@gmail.com>
wrote:

>
>
> po 7. 6. 2021 v 21:17 odesílatel Ron <ronljohnsonjr@gmail.com> napsal:
>
>> On 6/7/21 2:12 PM, David Gauthier wrote:
>>
>> Hi:
>>
>> I suspect I'm not the first to ask about this but couldn't find anything
>> after googling for a bit.  So here goes....
>>
>> I'd like to get the "age" difference between two times which span either
>> all or part of a weekend but exclude any time that transpired during the
>> weekend.
>> Example (please pardon the non-timestamp vals here...)
>>
>> age('Monday-Noon','Prev-Friday-Noon')
>> would give me '1 day'.
>>
>> ...and...
>>
>> age('Sunday-Noon','Prev-Friday-Noon')
>> would give me '12 hours'
>>
>> You get the picture.
>>
>> Has this wheel already been invented ?
>> I don't see an easy way to do this off-hand.
>> All Ears :-)
>>
>> Thanks in Advance.
>>
>>
>> You seem to be asking about counting work days.  Am I misunderstanding?
>>
>
> orafce has functions for business calendar  with holidays
>
> https://github.com/orafce/orafce
>
> plvdate.add_bizdays(day date, days int) date
>
> Regards
>
> Pavel
>
>
>> --
>> Angular momentum makes the world go 'round.
>>
>

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Need to omit time during weekends from age calculations
@ 2021-06-08 01:11  Sam Gendler <sgendler@ideasculptor.com>
  parent: David Gauthier <davegauthierpg@gmail.com>
  0 siblings, 0 replies; 7+ messages in thread

From: Sam Gendler @ 2021-06-08 01:11 UTC (permalink / raw)
  To: David Gauthier <davegauthierpg@gmail.com>; +Cc: Pavel Stehule <pavel.stehule@gmail.com>; Ron <ronljohnsonjr@gmail.com>; pgsql-general <pgsql-general@lists.postgresql.org>

On Mon, Jun 7, 2021 at 2:01 PM David Gauthier <davegauthierpg@gmail.com>
wrote:

> Thanks Joe.  I think the nonweekendhours solution should be good enough
> for what I need.
>
> Yes, holidays too would be the best.  But for practical purposes,
> excluding Sat&Sun is good enough for this particular problem.
>

I've solved this in the past with a time dimension table that includes
columns labeling weekends and holidays.  Then I can query for the count of
intervals (for whatever interval size my time dimension uses) in the date
range joined to time_dimension where weekday is true and holiday is false,
and multiply the count by the number of hours in an interval.

^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2021-06-08 01:11 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-06-07 19:12 Need to omit time during weekends from age calculations David Gauthier <davegauthierpg@gmail.com>
2021-06-07 19:17 ` Ron <ronljohnsonjr@gmail.com>
2021-06-07 19:45   ` Pavel Stehule <pavel.stehule@gmail.com>
2021-06-07 21:01     ` David Gauthier <davegauthierpg@gmail.com>
2021-06-08 01:11       ` Sam Gendler <sgendler@ideasculptor.com>
2021-06-07 19:34 ` Joe Conway <mail@joeconway.com>
2021-06-07 19:39   ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox