Re: [sqlalchemy] Count aggregation function with distinct over multiple columns

2018-12-14 Thread Mike Bayer
anything that looks like WORDS(, , , ...) that goes where SQL expressions go you can do with func. just use func.DISTINCT(col1, col2). On Fri, Dec 14, 2018 at 10:47 PM Pavel Pristupa wrote: > > I wonder if there's a way to build up the following SQL query for PostgreSQL > in SQLAlchemy using

[sqlalchemy] Count aggregation function with distinct over multiple columns

2018-12-14 Thread Pavel Pristupa
I wonder if there's a way to build up the following SQL query for PostgreSQL in SQLAlchemy using Session.query(): SELECT group_id, COUNT(DISTINCT(column1, column2)) FROM entities GROUP BY group_id; I tried this one: Session.query(Entity.group_id, sa.func.count(sa.distinct(Entity.column1,

Re: [sqlalchemy] .count() hangs indefinitely

2016-11-07 Thread mike bayer
On 11/07/2016 02:50 AM, James Burke wrote: Thanks Mike for your response. The query is run against a staging db and the table only contains some 500 records. But I will check the query as you have suggested to see what is going on. Well since it's just count() causing the problem here,

Re: [sqlalchemy] .count() hangs indefinitely

2016-11-06 Thread James Burke
Thanks Mike for your response. The query is run against a staging db and the table only contains some 500 records. But I will check the query as you have suggested to see what is going on. Cheers > > -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper

Re: [sqlalchemy] .count() hangs indefinitely

2016-11-06 Thread mike bayer
On 11/04/2016 04:13 AM, James Burke wrote: Hi All, I've run into a odd problem, where calling the count function hangs my code indefinitely. The odd thing is it was working until recently, so I'm a little confused. | customer

Re: [sqlalchemy] .count() hangs indefinitely

2016-11-06 Thread James Burke
> > > Thanks for your reply Simon. - I am using Postgresql database - Running the SQL generated by SQL Alchemy in Postgres also hangs. - There is no traceback. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please

Re: [sqlalchemy] .count() hangs indefinitely

2016-11-04 Thread Simon King
On Fri, Nov 4, 2016 at 8:13 AM, James Burke wrote: > Hi All, > > I've run into a odd problem, where calling the count function hangs my code > indefinitely. The odd thing is it was working until recently, so I'm a > little confused. > > customer = >

[sqlalchemy] .count() hangs indefinitely

2016-11-04 Thread James Burke
Hi All, I've run into a odd problem, where calling the count function hangs my code indefinitely. The odd thing is it was working until recently, so I'm a little confused. customer = session.query(Customer).filter(Customer.phone_number.contains([ message['metadata']['MIN']]))

[sqlalchemy] count subquery

2013-06-24 Thread Sebastian Elsner
Hello, I am trying to translate this SQL to a SQLAlchemy query, but failed so far: select `users`.`name`, `assignments`.`id`, ( select count(*) from `assignments` where `assignments`.`user_id` = `users`.`id` ) as `num_assignments` from `users` join `assignments` on

Re: [sqlalchemy] count subquery

2013-06-24 Thread Michael Bayer
it's a scalar subquery. you want to make the select() and then call as_scalar() on it so that it behaves like a column in a SQL expression. On Jun 24, 2013, at 9:34 AM, Sebastian Elsner sebast...@risefx.com wrote: Hello, I am trying to translate this SQL to a SQLAlchemy query, but failed

Re: [sqlalchemy] count subquery

2013-06-24 Thread Sebastian Elsner
Hm I'm lost. This is where I am now: session.query(User.id, Assignment.id, session.query(func.count(Assignment.id)).filter(Assignment.user_id == User.id).as_scalar()).join(Assignment).all() On 06/24/2013 04:26 PM, Michael Bayer wrote: it's a scalar subquery. you want to make the

Re: [sqlalchemy] count subquery

2013-06-24 Thread Michael Bayer
you might want to call correlate(User) on that scalar subquery also. paste the SQL along with it on the next email. On Jun 24, 2013, at 1:15 PM, Sebastian Elsner sebast...@risefx.com wrote: Hm I'm lost. This is where I am now: session.query(User.id, Assignment.id,

[sqlalchemy] count rows in a table

2011-10-25 Thread Mark Erbaugh
What's the recommended way to count the rows in a table. In SQL, I would typically use select count(*) from table; The statement session.query(table).count() issues a count(*) on a sub-query. The docs say for finer control to use func.count i.e. session.query(func.count(table.column)). That

[sqlalchemy] .count()'ing with limiting query.

2011-06-07 Thread Filip Zyzniewski - Tefnet
Hi everybody, After switching from SQLAlchemy 0.6.5 to 0.7.0 we have observed a problem with such an example code: (copy at http://ideone.com/WB36Y) == # Fails with Python-2.7.1 and SQLAlchemy-0.7.0 # import sqlalchemy import

Re: [sqlalchemy] .count()'ing with limiting query.

2011-06-07 Thread Michael Bayer
On Jun 7, 2011, at 8:57 AM, Filip Zyzniewski - Tefnet wrote: # this line: print BillGates.subordinates.count() # raises: # Traceback (most recent call last): # File count_problem.py, line 60, in module # print BillGates.subordinates.count() # File

Re: [sqlalchemy] Count over aliased JOIN

2010-11-24 Thread Michael Bayer
You should use func.count(expression) instead of query.count(). On Nov 23, 2010, at 1:52 PM, Juan Antonio Ibáñez juanito1...@gmail.com wrote: Hello I am joining one table with itself so I am using alias as: alias_usuarios1 = aliased(Usuario)

[sqlalchemy] Count over aliased JOIN

2010-11-23 Thread Juan Antonio Ibáñez
Hello I am joining one table with itself so I am using alias as: alias_usuarios1 = aliased(Usuario) alias_usuarios2 = aliased(Usuario) usuarios = DBSession.query(alias_usuarios1, alias_usuarios2.nombre).filter(alias_usuarios1.resellers_id == alias_usuarios2.id)

[sqlalchemy] count() not working?

2009-04-27 Thread DavidG
Hi, Ok, I am doing a standard tags thingee which is many-to-many. I want to to a query so that I get the main records which have all the named keywords. To use webpage paging efficiently, I need to get a count() of the records. This does not seem to work the way I am doing it. Also, I don't

[sqlalchemy] .count() returns wrong count

2008-04-10 Thread Dieter Verfaillie
Hello, I've created a single table inheritance hierarchy where SpecialThing inherits from BasicThing. When I session.query(BasicThing).count() the correct count is returned, but session.query(SpecialThing).count() returns the count for BasicThing. In other words, when issuing .count() the 'WHERE

[sqlalchemy] count() does not seem to work correctly with relationships

2007-12-21 Thread Justin
DB = sqlite I don't now if this is a bug or an issue with my mapper config, but the count() method does not work correctly through relationships. Given this mapper: mapper(Branch, branches, properties={ 'reports':dynamic_loader(Report, backref='branch') }) b = sess.query(Branch).first()

[sqlalchemy] count and distinct

2007-04-13 Thread vkuznet
Hi, I found that if I do select([func.count(table.c.column)],distinct=True).execute() the resulting query is select distinct count(column) from table but it's not what I wanted. If my column has duplicates you got counting them, rather then count unique names. The proper SQL query would be

[sqlalchemy] count(*) function

2007-01-15 Thread milena
Hi, does anyone know the syntax of SELECT COUNT(*) FROM table_name; for SQLAlchemy? Is there a list of functions (used in SELECT statement) that exist in SQL that I can use in SQLAlchemy? btw, I am using MySQL. I need to see if my table is empty (isemty function doesn't work). Thanks!

[sqlalchemy] count()

2006-12-08 Thread Alexandre CONRAD
Hello, I would like to query a big table and display the information. But at the top of the list I'm going to display, I need a little counter that tell how many items where returned. What would be the most efficient way to count the results ? client_list = session.query(Client).select()