Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering a sql count(*) with SQLAlchemy

How can I do the following SQL query using SQLAlchemy?

SELECT COUNT (*)
FROM det_factura
WHERE company = 31
  AND date::text LIKE '2011-05% ';
like image 876
leonciokof Avatar asked Jan 14 '12 20:01

leonciokof


1 Answers

If det_factura is also a mapped object, then this query should do it:

qry = (session.query(func.count(det_factura.id))
        .filter(det_factura.company==31)
        .filter(det_factura.date.like('2010-05%'))
        )

If it is a table instance, the one below should work:

qry = select([func.count(det_factura.id)],
        and_(det_factura.company==31,
             det_factura.date.like('2010-05%')
            )
        )
like image 133
van Avatar answered Sep 30 '22 00:09

van