How to build a Web2py query by quantifying the difference between days

Asked

Viewed 291 times

6

I’m using Web2py (Python), query using the DAL I wonder if it’s possible to pick up the difference in days, something close to that

Example

#!/usr/bin/env python   
# -*- coding: utf-8 -*-

  resultado = db(
    (db.base_suporte.dt_solicitacao - db.base_suporte.dt_fechamento) >5
  ).select(db.base_suporte.dt_solicitacao)

for curiosity the return of this query is

SELECT  base_suporte.dt_solicitacao FROM base_suporte WHERE (
    (base_suporte.dt_solicitacao - base_suporte.dt_fechamento) > 5.0);

I know how to do this using commands of each base using db.executesql(), but would like a solution using the DAL mechanism so that it is possible to maintain compatibility with other banks

I’m using individual record processing as an alternative solution.

  • brother, check this: http://stackoverflow.com/questions/1907088/how-can-i-make-between-query-with-web2py-dal

  • Fala ae @Samueldiogo, between picks record between a date is another, this would help but I need to make the difference of two dates, X and Y and check if it is greater than 5 days

1 answer

2

Isvaldo, I think what you want comes down to the following:

resultado = db(
        (db.base_suporte.dt_solicitacao - db.base_suporte.dt_fechamento) >5
      ).select(db.base_suporte.dt_solicitacao, db.base_suporte.dt_fechamento)

delta = db.base_suporte.dt_fechamento - db.base_suporte.dt_solicitacao
print delta.days

The return of any field of the type datetime is an object datetime of python and the subtraction of two objects of this type, returns a timedelta equivalent to subtraction of two dates and having the attribute days, corresponding to int of difference.

  • Oops, I think you got a little confused, we’re talking about the DAL class. DAL DOC

  • @Isvaldofernandes, did not confuse me not dude. Taking into account that db is an instance of DAL and that you set your table base_suporte in a similar way to... db.define_table('base_suporte', Field('dt_fechamento', 'datetime'), Field('dt_solicitacao', 'datetime'))&#My answer applies and I showed you how to return the difference between the two in o.o. days.?

Browser other questions tagged

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