Postgresql compare time between two hours, and if it exceeds the hours of the day, limit up to 23:59

Asked

Viewed 67 times

0

I’m making a query sql in the ruby-on-rails for PostgreSQL where I check if the user input is between hora_entrada and hora_entrada + (hora_entrada + limite_banco_horas) and the same goes for hora_fim, in a given dia. But if the hora_fim + limite_banco_horas past the hours of that day, the filter no longer brings the pessoas of the informed day. I need to limit the comparison until 23:59, case to soma com limite_banco_horas exceed this value, 23:59. Example:

SELECT "dias_trabalhados".* 
FROM "dias_trabalhados" 
INNER JOIN "presencas" ON "presencas"."id" = "dias_trabalhados"."presenca_id"
INNER JOIN "jornada_trabalhos" ON "jornada_trabalhos"."id" = "dias_trabalhados"."jornada_trabalho_id" 
LEFT JOIN "pessoas" ON "pessoas"."id" = "presencas"."pessoa_id"                 
LEFT JOIN "liberacao_bancos_hora" ON "liberacao_bancos_hora"."pessoa_id" = "pessoas"."id" AND "liberacao_bancos_hora"."data" = '2017-09-22' 

WHERE "presencas"."ano" = 2017 
AND "presencas"."mes" = 9 AND "dias_trabalhados"."dia" = 22 
AND (

     (
        "liberacao_bancos_hora"."limite" IS NOT NULL

        AND (

            OR (
                ("jornada_trabalhos"."hora_entrada" - ("liberacao_bancos_hora"."limite" ||' hours')::interval) >= '20:00'
                AND "jornada_trabalhos"."hora_entrada" <= '20:00'
            )

            OR (
                "jornada_trabalhos"."hora_saida" >= '22:00'
                AND ("jornada_trabalhos"."hora_saida" + ("liberacao_bancos_hora"."limite" ||' hours')::interval) <= '22:00'
            )
        )
     )
))

In this example, my query checks in the table dias_trabalhados all the pessoas who possess limite_bancos_horas registered, on the day 22/09/2017, amid 20:00 and 22:00. Suppose hora_saida of a pessoa be it 19:00, and she has 7h of banco de horas and may hit point until 01:00 of the day 23/09/2017.

And then I need to limit my filter in my query so that if the (hora_saida + limite_bancos_hora) past 23:59, I set the time for comparison until 23:59.

In the PostgreSQL there is a function for it, or how do I limit until the end of the day?

  • I noticed that your consultation has a clause WHERE within the condition ON of LEFT JOIN with the table liberacao_bancos_hora. Your query is correct ?

  • is working, sorry, it was time to index here in the text edition, I will arrange, thank you!

  • I understand your problem! However, so that I can elaborate a more appropriate answer to your case, it would be interesting for you to include in your question the structure of the tables that are part of your query. It is possible ?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.