Thanks for reply, Mariano.
j

On 05/23/2013 12:37 PM, Mariano Mara wrote:


On 05/23/2013 04:42 AM, jo wrote:
|Hi all,

I wondered if it is possible to execute a partial distinct in sqlalchemy.
The following query works in oracle and postgresql:

select distinct col1, first_value(col2) over (partition by col1 order by
col2 asc)
from tmp;

How can I do such query in sqlalchemy?
Thanks for any help.
j

Yes, it is entirely possible. Something like this should do the trick (not tested):

>>> from sqlalchemy import select, func

>>> from sqlalchemy.sql.expression import over

>>> q = select([tmp.c.id.distinct(), over(func.first_value(tmp.c.cid), partition_by=tmp.c.id, order_by=tmp.c.name.asc())])

>>> print(q)
SELECT DISTINCT "user".id, first_value("user".cid) OVER (PARTITION BY "user".id ORDER BY "user".name ASC) AS anon_1
FROM "user"

This chapter of the documentation will help with these features and much more: http://docs.sqlalchemy.org/en/latest/core/expression_api.html


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


Reply via email to