Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

335
Views
Cómo agregar horas de intervalo a una marca de tiempo en postgres, excluyendo las horas de fin de semana

tengo una tabla en postgres con 2 columnas; order_timestamp, semana_horas.

Quiero crear una tercera columna 'due_timestamp' que se calcula usando 'ordered_timestamp' más 'weekday_hours' ... PERO excluyendo las horas que caen durante el fin de semana (12:00 a.m. sábado a 12:00 a.m. lunes)

 ordered_timestamp | weekday_hours | due_timestamp 2020-06-04 16:00:00 | 12 | 2020-06-05 04:00:00 2020-06-05 16:00:00 | 48 | 2020-06-09 16:00:00

Sin la advertencia de excluir las horas de fin de semana, podría usar ordered_timestamp + interval '1 hour' * weekday_hours

weekday_hours puede variar de 1 hora a cientos de horas en algunos casos.

Parece que esta situación necesita algo adicional para filtrar las horas de fin de semana.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Si sus intervalos de fechas pueden abarcar varios períodos, usaría un enfoque de fuerza bruta enumerando todos los intervalos de 1 hora que caen entre las marcas de tiempo con generate_series() y luego contando solo aquellos que pertenecen a días hábiles:

 select t.ordered_timestamp, t.due_timestamp, x.weekday_hours from mytable t cross join lateral ( select count(*) filter(where extract(dow from ts) between 1 and 5) - 1 weekday_hours from generate_series(ordered_timestamp, due_timestamp, '1 hour'::interval) s(ts) ) x

Demostración en DB Fiddle :

orden_marca de tiempo | debido_marca de tiempo | semana_horas
:------------------ | :------------------ | ------------:
2020-06-04 16:00:00 | 2020-06-05 04:00:00 | 12
2020-06-05 16:00:00 | 2020-06-09 16:00:00 | 48
over 4 years ago · Santiago Trujillo Report

0

¿algo como esto?

 case when (EXTRACT(DOW FROM ordered_timestamp ) = 6 ) then ordered_timestamp + interval '1 hour' * (weekday_hours+48) when (EXTRACT(DOW FROM ordered_timestamp ) = 0 ) then ordered_timestamp + interval '1 hour' * (weekday_hours+24) else ordered_timestamp + interval '1 hour' * weekday_hours end
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!