I dont see any "COUNT", OVER, LIMIT, or anything else in that SQL statement, so 
I dont see why you're using func.count().over(), limit(), offset(), etc.

The query as written is equivalent to "SELECT DISTINCT col1, col2, col3 FROM 
mytable WHERE col1='something'",  which is:

from sqlalchemy.sql import column, table, distinct
from sqlalchemy.orm import Session

mytable = table('mytable', column('col1'), column('col2'), column('col3'))
print Session().query(
        mytable.c.col1, 
            mytable.c.col2, 
            mytable.c.col3).\
            distinct().\
        filter(mytable.c.col1=='something')

but this is not what you asked for earlier, so I suspect this is not 
specifically what you want.

The PG dialect supports special extensions for DISTINCT which allows DISTINCT 
ON (list of columns), if you pass a list of them to query.distinct(), for 
example.



On May 16, 2012, at 12:13 PM, Eduardo wrote:

> 
> Hi,
> I am trying to write this SQL coomand:
> 
> SELECT DISTINCT(col1, col2,col3) FROM mytable WHERE col1='something' 
> 
> in sqlalchemy form:
> 
> columns=['col1','col2','col3']
> 
> rows = session.query(*[func.count().over().label("count")]+map(lambda column: 
> MyClass.__dict__[columns],columns)).filter(...).limit(n).offset(m).all()
> 
> this query works it returns the results together with the number of hits.
> The problem is that when I apply the distinct() command
> rows = session.query(*[func.count().over().label("count")]+map(lambda column: 
> MyClass.__dict__[columns],columns)).filter(...).limit(n).offset(m).distict().all()
> 
>  func.count().over().label("count") does not show the correct number of hits 
> because func.count() relates to all column in table and to those selected - 
> that is col1, col2 and col3
> My question is how to write the count function that will correctly count 
> number of distinct hits but only for the selected columns.
> I tried with:
>  
> func.count().over(partition_by=MyClass.col1,MyClass.col2,MyClass.col3).label("count")
> 
> and it did not work
> 
> If I want to use:
> 
>  
> func.count(distinct(MyClass.col1,MyClass.col2,MyClass.col3)).over().label("count")
> 
> only one column is allowed as the input argument for the count() function.
> 
> 
> Is there any way to solve this?
> Thanks 
> 
> 
> Am Mittwoch, 16. Mai 2012 12:07:26 UTC+2 schrieb Eduardo:
> Hello,
> I have got a query of the following type:
> 
> rows = session.query(*[func.count().over().label("count")]+map(lambda column: 
> MyClass.__dict__[columns],columns)).filter(...).limit(n).offset(m).all()
> 
> it returns the number of results together with values of selected columns. 
> The problem is when I try to apply the distinct query I see that 
> func.count().over().label("count") does not return correct results, that is, 
> it returns the number of results for the query by which the distinct function 
> is omitted.
> Is there any workaround for this problem?
> The query contains no joins.
> Thank you
> ED
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To view this discussion on the web visit 
> https://groups.google.com/d/msg/sqlalchemy/-/11I7cmn-UaoJ.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to