Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

339
Visualizações
How to add interval hours to a timestamp in postgres, excluding weekend hours

I have a table in postgres with 2 columns; ordered_timestamp, weekday_hours.

I want to create a third column 'due_timestamp' which is calculated using 'ordered_timestamp' plus 'weekday_hours' ...BUT excluding the hours that fall across the weekend (12:00am Sat to 12:00am Mon)

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

Without the caveat of excluding weekend hours, I could use ordered_timestamp + interval '1 hour' * weekday_hours

weekday_hours can vary from 1hr to hundreds of hours in some cases.

It seems this situation needs something additional to filter out the weekend hours.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If your date intervals may span over variying periods, I would use a quite brute force approach by enumarating all 1 hour intervals that fall between the timestamps with generate_series(), and then counting only those that belong to working days:

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

Demo on DB Fiddle:

ordered_timestamp   | due_timestamp       | weekday_hours
:------------------ | :------------------ | ------------:
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 Relatório

0

something like this?

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda