Re: [sqlalchemy] Distinct within group by query

2019-09-05 Thread Andrew M
Yes, I'm using Postgres. This does exactly what I need: session.query(Product.attribute_x, func.min(Product.price), func.array_agg(func.distinct(Product.color))).group_by(Product.attribute_x) Thanks Varun, that's saved me a real headache. I appreciate your help. -- SQLAlchemy - The Python

Re: [sqlalchemy] Distinct within group by query

2019-09-04 Thread Varun Madiath
What database are you using? Some databases might support this natively, others would have to be emulated by the ORM. I’m not a big user of the ORM, so someone else would have to help you with that. If you’re using Postgres, then the array_agg function on the color column will give you exactly

Re: [sqlalchemy] Distinct within group by query

2019-09-04 Thread Andrew M
Thank you Varun - what you've shown in the table is exactly what I'm looking for. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See

Re: [sqlalchemy] Distinct within group by query

2019-09-04 Thread Varun Madiath
Just so I understand what you're asking? You want the minimum price per product (across all colors), as well as the list of colors? A result set looking something like this? | Attribute | Min Price | Colors | |---|---|--| | Attr_1| 10|

[sqlalchemy] Distinct within group by query

2019-09-04 Thread Andrew M
Hi, I would like to use distinct inside a group_by query, e.g.: session.query(Product.attribute_x, func.min(Product.price), distinct(Product.color)). group_by(Product.attribute_x) That is, for each value of attribute_x, I want to find the lowest price and all of the colors available. I don't

Re: [sqlalchemy] .distinct() silently ignored for DBs other than Postgres

2017-05-30 Thread mike bayer
On 05/30/2017 07:42 AM, Christopher Wilson wrote: Dear Michael and fellow SQLAlchemy users/developers, I think I have found an issue with the use of distinct() over specified columns on a Query: http://docs.sqlalchemy.org/en/latest/orm/query.html#sqlalchemy.orm.query.Query.distinct As the

[sqlalchemy] .distinct() silently ignored for DBs other than Postgres

2017-05-30 Thread Christopher Wilson
Dear Michael and fellow SQLAlchemy users/developers, I think I have found an issue with the use of distinct() over specified columns on a Query: http://docs.sqlalchemy.org/en/latest/orm/query.html#sqlalchemy.orm.query.Query.distinct As the Postgres documentation notes, this is a non-standard

Re: [sqlalchemy] DISTINCT with LIMIT problem

2013-05-31 Thread Kent
Thanks very much! I got it to work apparently fine using from_self(). I didn't seem to need anything special for eager loads to continue to function... were you only expecting I'd have troubles with eager loads if I used subquery()? On 5/30/2013 6:29 PM, Michael Bayer wrote: On May 30,

Re: [sqlalchemy] DISTINCT with LIMIT problem

2013-05-31 Thread Charlie Clark
Am 30.05.2013, 23:19 Uhr, schrieb Kent jkentbo...@gmail.com: For example, a query may look like this: select distinct count(*) over () as recordcount, tablea.colx, tableb.coly from tablea, tableb where limit 100 This doesn't *quite* work because the analytical window function

Re: [sqlalchemy] DISTINCT with LIMIT problem

2013-05-31 Thread Kent
I allow the user to join with other tables for the purpose of filtering (even though the joined tables won't be selected). Cartesian is probably the wrong term for the effect, but in the end, I get duplicate rows. I could get rid of the need for distinct by extensively using EXISTS clauses

[sqlalchemy] DISTINCT with LIMIT problem

2013-05-30 Thread Kent
We use func.count().over() in order to help support result pagination. When attempting to limit the result set, I have found that if other tables are being joined (for the where clause, but not selected), then I need to add DISTINCT to the query or else the Cartesian result of my query messes

Re: [sqlalchemy] DISTINCT with LIMIT problem

2013-05-30 Thread Michael Bayer
On May 30, 2013, at 5:19 PM, Kent jkentbo...@gmail.com wrote: Solution A: Group by all columns (yielding the same effect as distinct), but which makes the window analytical function process after the group by and yields the correct count (17 instead of 72): are all

Re: [sqlalchemy] DISTINCT with LIMIT problem

2013-05-30 Thread Kent
Thank you, I'll try that, but quick concern: I specifically skipped trying to use .subquery() because the docs say Eager JOIN generation within the query is disabled. Doesn't that mean I won't get my joinedload() results from the inner query? Or does that refer to the outer query having

Re: [sqlalchemy] DISTINCT with LIMIT problem

2013-05-30 Thread Michael Bayer
On May 30, 2013, at 6:06 PM, Kent jkentbo...@gmail.com wrote: Thank you, I'll try that, but quick concern: I specifically skipped trying to use .subquery() because the docs say Eager JOIN generation within the query is disabled. Doesn't that mean I won't get my joinedload() results from

Re: [sqlalchemy] distinct on

2013-05-24 Thread Jose Soares
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

[sqlalchemy] distinct on

2013-05-23 Thread jo
|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

Re: [sqlalchemy] distinct on

2013-05-23 Thread Mariano Mara
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

Re: [sqlalchemy] distinct query

2010-03-19 Thread Sebastian Elsner
Hello, qry = session.query(AssetCategory).join(Asset).join(Shot).filter(Shot.id==1).distinct() this one already did the trick. qry = qry.filter(Shot.id==shot_id_of_interest) what did you add this for? The results seem to be identical... that generates SELECT DISTINCT AssetCategory.id

[sqlalchemy] distinct query

2010-03-18 Thread Sebastian Elsner
Hello, I am stuck here with a query, it't not too complicated I think, but I dont get it... I have three Class/table mappings: Shots, Assets, which belong to a Shot (1:n) and AssetCategories, which are owned by Assets (n:1) The objective is: For a given shot instance get all distinct

Re: [sqlalchemy] distinct query

2010-03-18 Thread Mike Conley
Did you try qry = session.query(AssetCategory).join(Asset).join(Shot).filter(Shot.id==1).distinct() qry = qry.filter(Shot.id==shot_id_of_interest) that generates SELECT DISTINCT AssetCategory.id AS AssetCategory_id FROM AssetCategory JOIN Asset ON AssetCategory.id = Asset.category_id JOIN Shot

[sqlalchemy] distinct on two fields with a count

2010-03-11 Thread Moshe C.
How do I write an expression (in sqlalchemy 0.4.6) that will generate the following expression: select count(distinct field1, field2) from tableA; I know how to do it for one field and I know how to do it with no count using distinct=True, but not this combination. -- You received this

Re: [sqlalchemy] distinct on two fields with a count

2010-03-11 Thread Moshe Cohen
Thanks. The DB is MySQL. On Fri, Mar 12, 2010 at 2:16 AM, Conor conor.edward.da...@gmail.com wrote: Moshe C. wrote: How do I write an expression (in sqlalchemy 0.4.6) that will generate the following expression: select count(distinct field1, field2) from tableA; I know how to

[sqlalchemy] distinct (or group by) with max in sqlalchemy - how to?

2009-09-04 Thread Nguyen, Thang P
Hi, I've recently started learning sqlalchemy. So far, I am able to do everything I needed with sqlalchemy query. Now, I am getting into a situation, and hope someone would give me a suggestion. Below is my question. My test table has two columns Name and Count Name Count A

[sqlalchemy] distinct entries when mapping many-to-many

2007-12-05 Thread MattQc
I am mapping 3 tables linearly. The first 2 have a many-to-many relation and the last one is a one-to-one relation. I would like to get all the distinct entries from the third table. I was not able to figure how to do the mapping. So, I tried by doing my own set of entries outside of SA but when

[sqlalchemy] DISTINCT in select_by() ?

2006-11-19 Thread ram
Is it possible to specify a DISTINCT option to select_by()? Couldn't find it in the docs. In general, what select() options does select_by() take, if any? Thx, Rick --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups